This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
I have the following tables: CREATE TABLE `site` ( I'm using the following code: var _context = new Mindscape.LightSpeed.LightSpeedContext(); a site object can have multiple users, and a user can exist at multiple sites. the problem I'm having is that it doesn't want to let me create a site_user row referencing a site and a user that I have just created, but not committed to the database. the exception I'm getting is as follows: An error occurred when trying to assign a new Identity value to this entity. Check inner exception for details. Inner Exception: Invalid cast from 'System.Int32' to 'LSTest.SiteUserId'. |
|
|
Composite identities where the components are also foreign keys requires some special and slightly delicate handling. See http://www.mindscape.co.nz/forums/Thread.aspx?PostID=9693 and the slightly more up-to-date http://www.mindscape.co.nz/forums/Thread.aspx?ThreadID=3179 for more info: if these don't provide enough info to get it working, please post your SiteUser class and the stack trace of the exception and we'll try to track down the problem. |
|
|
I looked at the two threads you referenced, but didn't really understand how they help with my situation. the following are the classes that make up the model:
[Serializable]
The call stack doesn't contain a lot of useful data: LSTest.exe!LSTest.Program.Main(string[] args = {string[0]}) Line 88 C# [Native to Managed Transition] [Managed to Native Transition] Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x47 bytes mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x9b bytes mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x4d bytes
|
|
|
It looks like the problem is that SiteUser doesn't have a way to determine its ID. Because SiteUser has a composite key, it derives from Entity<SiteUserId>, which means its ID is of type SiteUserId (which is in turn a little struct with a field for each primary key column). However, your identity method (probably KeyTable) is generating IDs of type int. So when you create a SiteUser, LightSpeed assigns an ID of type int, and tries to set SiteUser.Id to that int, which fails because an int is not a SiteUserId. The solution to this is to create a partial class for SiteUser and override the GeneratedId() method so that it returns a SiteUserId instead of an int. To learn how to do this, see: http://www.mindscape.co.nz/blog/index.php/2009/12/22/composite-keys-in-lightspeed-3/ and in particular the section "Assigning Composite IDs." Once you've got the composite key working, you'll then need to set up the foreign key mappings so that the associations are wired up to the Id.Xxx fields -- the previous links I posted should help with that. Hope this helps -- let us know if you need further info. |
|
|
When I add the ForeignKeyField attributes to the fields in the SiteUser class like so: [ForeignKeyField("Id.IdUserId")]
it complains with the exception "Circular associations are not supported by LightSpeed: [SiteUser.IdUser and User.SiteUsers]"
it gives me this exception with or without the ReverseAssociation attributes. |
|
|
That's a very misleading error message, and we'll see if we can get it to produce something more meaningful, but what it's telling you is that your foreign key mappings are invalid. There are two issues that need to be addressed: 1. SiteUser shouldn't contain IdUserId and IdSiteId properties, because these columns are mapped into the composite Id (the SiteUserId struct). You may already have removed these -- just going on your earlier code fragment here. 2. The ForeignKeyFieldAttribute paths don't correspond to the properties in the SiteUserId struct. For example, your User association has ForeignKeyField("Id.IdUserId"); but SiteUserId doesn't have an IdUserId property, it has an IdUser property. So your attributes (or column mappings in the designer) should be "Id.IdUser" and "Id.IdSite" i.e. without the additional Id suffix. I've attached my updated version of your model, which works correctly for me -- you should be able to diff this against your code to see what I needed to change. Hope this makes sense -- let me know if you want further clarification. |
|
|
due to the amount of extra work involved in composite primary keys which are also foreign keys, I've decided to go a different route that will work just as well with less difficulty. the new site_user table is as follows:
CREATE TABLE `site_user` (
this provides all the functionality that I am looking for, with no extra work.
with the new schema design, it's a simple matter to create an empty site_user and add it to the SiteUsers collection in both the user and the site, and then add the site and user objects to the database. the id for site_user gets generated automatically as it should, and the foreign key values get hooked up as they should.
in my search for a viable .Net data provider for MySql, I also tried the Devart DotConnect system, which takes those composite key tables and converts it to a collection of site objects in the user, and a collection of user objects in the site. the site_user table did not show up as an object in the model at all. it might be worth looking into that sort of arrangement for Lightspeed in a future release. |
|
|
Yes, if you have control over the database design then adding a separate PK column definitely makes linking tables easier to work with in LightSpeed. Thanks for the suggestion on hiding the site_user table. We can already do this in certain circumstances -- the Through Association arrow has an option for "auto through entity" which hides the link entity -- but it has certain limitations which mean it's not suitable for your schema. (Specifically, an auto through entity doesn't support a custom table name mapping.) More information at http://www.mindscape.co.nz/blog/index.php/2009/09/17/many-to-many-associations-in-the-lightspeed-designer/ |
|