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
|
Hiya
I have 2 tables, Staff and Location, which join with many to many table of StaffLocation. This table has the id's from both tables and the fk's are setup correct. I can drag the StaffLocation table onto the designer and configure a Through Association just fine. This generates StaffLocation<StaffLocationId> and a StaffLocationId struct. I then extended StaffLocation as per the help file thus:
public partial class StaffLocation { [Transient]
public StaffLocation(int staffId, int locationId) { _pendingId = new StaffLocationId(staffId, locationId);
return _pendingId; I needed to add the parameterless constructor - it failed without this. If I try to insert a staff member to a location via the through association I get : An error occurred when trying to assign a new Identity value to this entity. Check inner exception for details ---> System.InvalidCastException: Invalid cast from 'System.DBNull' to 'BlahBlah.StaffLocationId'..
Am I missing something here?
Thanks |
|
|
Hmm, ok, looking at other threads here it looks like these composite tables are not going to work. I hope I'm wrong there cause we cant change the db. If not, then the help file needs updating - it's confusing and slightly misleading. Better yet, it would be great if you could get composites to work betterer :) Cheers |
|
|
Rats, still stuck on this. I followed Ivans blog post but that doesnt make sense to me and I cant get it to work. Does anyone have some working sample code for a composite key?
Thanks |
|
|
Peter, I think your problem is the same as dsapala's -- namely that LightSpeed does not directly support associations where the foreign key is also the Id, or part thereof. (There is a bug in the designer where it does create the associations if you drag and drop from the database when the table has a composite primary key and one of the PK columns is also a FK. It shouldn't be doing this because the runtime doesn't support it.) I've posted some info and links to possible workarounds at http://www.mindscape.co.nz/forums/Post.aspx?ThreadID=2991&PostID=9494, including what you'll need to do to get this working as a through association (sneak preview: you'll need to change the database). We are noting quite a few customers wanting support for CKs that are built on FKs so we will bump the priority in this but I can't make any promises. |
|
|
Thanks Ivan. There are a few things in 'our' database like this and the 'owner' is very stubborn about changing the schema. We will work around for now.
Cheers |
|
|
Righty ho, we have now implemented experimental support for associations based on composite key fields (i.e. where fields of the composite key are also foreign keys). This will be included in nightly builds dated 23 Mar 2010 and above, available from about 1500 GMT. To use this, you must apply the new ForeignKeyFieldAttribute to the EntityHolder end of the association, with a string of the form "Id.YourField" where YourField is the name of the field in the composite key e.g. in Peter’s case it might be "Id.StaffId", in dsapala’s case it might be "Id.UserId": [ForeignKeyField("Id.UserId")] Note that you must NOT also have a field of the same name in the entity. The field must appear in the composite key struct ONLY. Now on to some consequences and limitations. First, this is EXPERIMENTAL support. We have tested the core cases, of course, but we have not had time to test it as comprehensively as our "normal" association support. So there are likely to be rough edges and at least a few strange errors. Please be patient and let us know if you find things that don’t work correctly. Second, this is not yet supported in the designer. You will therefore need to remove any associations of this kind from the designer (or set their Generation to None) and implement them in the partial classes. I will try to get support into the designer reasonably quickly but it will not be in the forthcoming nightly. Third, remember that these associations become part of the identity, and the identity in LightSpeed is immutable. You cannot change an association after the entity has been created. Therefore you will want to make your association properties read-only: public User User { If you leave the setter in, and call it, then LightSpeed will throw an exception. Fourth, note that this is limited to associations where the key is a single column. You cannot use a multi-column subset of the Id as a foreign key. (You can of course have several associations based off different columns of the Id, e.g. dsapala’s User and Account associations or Peter’s Staff and Location associations. But each association may involve only one column.) Please download the nightly when it’s available and let us know how this works out for you. Again, I need to stress that this is a first cut and you may run into errors, rough edges or things that just don’t work -- we will of course aim to fix any reported issues promptly. Thank you for your feedback! |
|
|
I havent forgotten you, I'm testing this out now with monday's nightly.
|
|
|
So, progress of sort of. I have 2 tables - Package, Address - and PackageAddress to associate them. The same address can be used for multiple packages and a package may have many addresses (delivery, billing etc). PackageAddress has an id of PackageAddressId which contains just the two Id fields from Package and Address (both Guids). The designer created this nicely. I could not add the ForeignKeyField attribute to the entityholders via the model - there is no property do this. Instead, I created a partial class to extend the PackageAddress entity and I set the Generation option on the two relationships to None. I then also needed to create partials for Package and Address to have the back references. This was easy - I really just moved the code out of the designer code behind file.
Now, when I test this thus :
pkg.PackageAddresses.Add(new PackageAddress() { Package = pkg, Address = makeAddress()}); and try to save this I get an error:
Mindscape.LightSpeed.LightSpeedException: An error occurred when trying to assign a new Identity value to this entity. Check inner exception for details ---> System.InvalidCastException: Object must implement IConvertible..
System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
SetIdInternal(Object )
SetIdInternal(Object )
get_Id()
get_IdInternal()
b__1()
Mindscape.LightSpeed.Entity.SetInternal(IEntityHolder , Entity )
Mindscape.LightSpeed.Entity.Set[TEntity](EntityHolder`1 entityHolder, TEntity value)
...
I can email you the code if you like.
Cheers
|
|
|
Yes, if you could send us the code then that would be handy. You can attach a zip file via the Options tab, or email ivan @ the obvious domain name if it's sensitive (please remove all binaries first). You're right, we haven't yet updated the designer to support ForeignKeyField so the partial class workaround is what you'll need to do for now. |
|