Child Entities Not Updating

Aug 23, 2011 at 4:29 PM

I am trying to get two entities to update, but it only chooses to update one.  I have two tables one called MemberAsset (Id, MemberId, AssetId) and Asset (Id, Active, DateCreated).  AssetId in MemberAsset is a FK with the Id of Asset. 

 

var options = new DataLoadOptions();
            options.LoadWith<MemberAsset>(q => q.Asset);

            var memberAsset = UserAssetRepository.Get(options, q => q.MemberId == memberId).FirstOrDefault() ??
                             new MemberAsset() {Asset = new Asset()};

 

The MemberAsset comes back with the attached Asset. 

 

memberAsset.SetAsChangeTrackingRoot(true);
                memberAsset.SetAsUpdateOnSubmit(true);

My save method is below. The child entity is added to _cacheAssociationProperties

 using (var context = GetDataContext())
            {
                context.DeferredLoadingEnabled = false;
                entity.SynchroniseWithDataContext(context, true);

                if (entity.Id == default(int))
                {
                    context.MemberAssets.InsertOnSubmit(entity);
                }

                context.SubmitChanges();

                return entity.Id;
            }
Is there something I am missing? The method below addes it to the FK cache which never gets trans-versed to be updated.
if (assocAttribute.IsForeignKey != true)
                            _cacheAssociationProperties[type].Add(propInfo.Name, propInfo);
                        else
                            _cacheAssociationFKProperties[type].Add(propInfo.Name, propInfo);

 

Coordinator
Aug 24, 2011 at 10:24 AM

Hi there,

LINQ to SQL Serialization works in one direction (from Parent --> Child).

In your case I can see that Asset is the "Parent" and MemberAsset is the "Child" because of where the foreign key lives, in other words - a MemberAsset cannot exist without an Asset (unless the relationship is optional).

You should see in the DMBL diagram the following:

Asset --> MemberAsset

In your Scenario to use the LINQ to SQL serialization properly you need to have Asset as your Change tracking root, not the member Asset.

If this is not your intention, you should change your data model as your relationship may be the wrong way round.

Hope that makes sense.

Cheers

Matt.

Aug 24, 2011 at 8:36 PM

Member Asset contains Id, AssetId, and MemberId.  It is referenced to an Asset and a Member.  That would make it a parent.  I did it the other way before you answer and yes it worked, but it just doesn't make sense when I am trying to update a MemberAsset.