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
|
Hi, I'm creating new objects which have a parent/child (foreign key) relationship to their own types using a nested "for" loop. As I am creating them I am adding them to my unit of work using uow.Add(). At the end of the the nested for loops I am calling uow.SaveChanges(). The objects are being created in the database but their foreign key column is NULL. Can anyone explain how I can get around this so the foreign key column is not set to null? I am using "KeyTable" for identity generation. When I'm debugging I can see that the entitystate = new and Id = 0, but I was under the impression that using "KeyTable" would acquire a unique key before the object was commit to the db. Here's my code:
And here's my object PhysicalLocation: [Table("PhysicalLocations")] Any help would be greatly appreciated. Thanks in advance. |
|
|
|
|
It looks like you don't have a foreign key field for the ParentLocation association, so the ParentLocationId column isn't getting mapped and therefore isn't getting saved. Add the following members to the PhysicalLocation class: private int? _parentLocationId; LightSpeed will automatically wire this up to the ParentLocation association based on the naming convention. Note that the field must be nullable. Regarding KeyTable: that is correct. In fact, KeyTable will assign an ID to the entity as soon as it is added to a unit of work. |
|
|
|
|
Thanks Ivan, Right after I posted my code I realized I did not have any relationships defined. So I added the following code and got it working like a charm! I wanted to post a reply to my own message but I guess my post had not yet been approved because I did not see the "reply" button.
#region Relationships [ReverseAssociation("ChildLocations")]
public EntityHolder<PhysicalLocation> _ParentLocation = new EntityHolder<PhysicalLocation>();
public PhysicalLocation ParentLocation
{
get { return Get(_ParentLocation); }
set { Set(_ParentLocation, value); }
}
[Column("ParentLocation")] private int? _ParentLocationID;
public int? ParentLocationId
{
get { return _ParentLocationID; }
set { Set(ref _ParentLocationID, value); }
}
public readonly EntityCollection<PhysicalLocation> _childLocations = new EntityCollection<PhysicalLocation>();
public EntityCollection<PhysicalLocation> ChildLocations
{
get { return Get(_childLocations); }
}
#endregion
I also did some quick test between using the KeyTable identity generation strategy and using letting the DB create the keys.. the difference is performance is truly stunning. I think I'd originally left out the code above because I thought LightSpeed might be able to figure out on its own that the object has relationships on itself. Not that it's a big issue but would it be possible in the future to have this wiring up fully automated? Thanks. |
|
|
|
|
[quote user="SR8"]I think I'd originally left out the code above because I thought LightSpeed might be able to figure out on its own that the object has relationships on itself. Not that it's a big issue but would it be possible in the future to have this wiring up fully automated? Thanks.[/quote] This probably won't be possible for hand-coded entities, but it is handled for you if you use the designer. |
|
|
|