How to update / change LINQ EntitySet?

In my generated LINQ to SQL classes, I have classes containing EntitySets.

Everything looks great until I need to modify the EntitySet (add, remove some relationships). I thought it would work by doing something like:

User.Actions = newUserActions;   //This is how I used it with NHibernate

Then, when I try to make changes, I can get a duplicate key exception, which means that it does not clear the old actions before assigning new ones, and some of them can be repeated; Do I need to do this manually?

What is the best way to do this (update EntitySet)? Suggestions?

Thnx

+3
source share
3 answers

, . (EntitySet):

//The EntitySet is user.UserActions
DataBaseContext.UserActions.DeleteAllOnSubmit(user.UserActions);
DataBaseContext.SubmitChanges();

"" EntitySet:

user.UserActions.Assign(GetSelectedActions());
DataBaseContext.SubmitChanges();

, ... 2 SubmitChanges() ? ?

+2

?

user.UserActions.Clear();
user.UserActions.AddRange(GetSelectedActions());
context.SubmitChanges();
+1

In a SQL database relationship, do you have a set of cascading deletes? To do this, you will need this set and regenerating classes.

0
source

Source: https://habr.com/ru/post/1733117/


All Articles