Removing Item From an EntitySet

Aug 3, 2009 at 10:41 AM

firstly thank u very mush for sharing your disconnected solution it as help me very much.

I am running a POC to check this solution.

My goal is to imlement a solution were develpoers only need to tell call the 

SetAsChangeTrackingRoot Method.

change their entity as they where connected.

and call the SyncornizeDataContext on server side

I have trobule with the follwing  delete scenario.

Entity X as an EntitySet of Entity Y both inheart from LINQEntityBase

I want to remove one  y from x this means x is updated so i call

X.SetAsChangeTrackingRoot(true);

X.Ys.remove(Y);

This make Y.EntitySate = Deatched;

when i call

X.SynchroniseWithDataContext

I get A Excpetion.

Message = "An entity can only be attached as modified without original state if it declares a version member or does not have an update check policy." 

Should I Change the SynchroniseWithDataContext to deal with this Deatched state ?

Or maybe change the PropertyChanging logic to mark the State to Deleted For Y ?

Or I am missing somthing. ?

 

Thnx

Yaki.

 

Aug 3, 2009 at 12:55 PM

Hi there Yaki,

Thanks, I think you've found a bug!

I had a quick look and haven't been able to make any changes, however I think I can see how to fix it.

If you find the following line in the "LINQEntityBase.PropertyChanged" method file:

LINQEntityState = EntityState.Detached;

and insert this line before or after it, it should be ok...

this._originalEntityValue = this._originalEntityValueTemp;

Try it out and let me know how it goes.

I'll fix the source code properly on the weekend.

Cheers

Matt

Aug 3, 2009 at 1:30 PM

Hi

Thnx For Your Quick reply

Now In the SynchroniseWithDataContext  we go in  targetDataContext.GetTable(entity.GetEntityType()).Attach(entity, entity.LINQEntityOriginalValue);

instead of

<font size="2">

targetDataContext.GetTable(entity.GetEntityType()).Attach(entity,

</font>

true);

But Still we  attach  instead of DeleteOnSubmit

Ex:

An attempt was made to remove a relationship between a X and a Y . However, one of the relationship's foreign keys (Y.XID) cannot be set to null.

 

maybe the better solution is to on Remove To Set the LinQEntityState to Deleted the right state SynchroniseWithDataContext  should handle ?

regards

Yaki.

 

 

Aug 3, 2009 at 2:01 PM
Edited Aug 3, 2009 at 2:03 PM

Hi Yaki,

Is it that you would like to detach or delete?

The remove function is for removing relationships between objects only - it does not delete any objects (see the LINQ to SQL Documentation).

The error message you are getting now is because the foriegn key on Y to the X entity is not allowed to be set to NULL (according to the properties in your DBML) - it is trying to remove the relationship between the entities.

If you want to allow detachments, you will need to change your DBML so that the XID field can be null.

If you want to delete, you must use the SetAsDeleteOnSubmit() function, this is the only way to do it.  The object will not be removed from the collection until the SyncroniseWithDatabase() is called, however you could filter temporarily it out by using something like:

var noDeletedRecords = X.Ys.Where( x = > x.LINQEntityState != EntityState.Detached);

Cheers

Matt.

Aug 3, 2009 at 3:14 PM

 

Hi Matt
 
I checked the connected behavior  (which is our goal )   of remove
and same error was thrown :-)
 
ok i will use SetAsDeleteOnSubmit
 
thnx  yaki