如何更新/修改 LINQ EntitySet?

发布于 2024-08-22 13:08:31 字数 329 浏览 4 评论 0原文

在我的 LINQ to SQL 生成的类中,我有一些包含 EntitySet 的类。

一切看起来都很棒,直到我需要修改 EntitySet(添加、删除一些关系)。 我认为这样做会起作用:

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

然后,当我尝试提交更改时,我可能会收到重复的键异常,这意味着在分配新操作之前不会清除旧操作,并且其中一些操作可能会重复;我必须手动完成吗?

执行此操作的最佳方法是什么(更新实体集)?建议?

谢谢

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

Everything looks great until I need to modify the EntitySet (add, delete some relationships).
I thought that it was going to work doing something like:

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

Then when I try to submit changes, I may get a duplicate key exception, that means it is not clearing the old actions before assigning the new ones and some of them may be repeated; do I have to do it manually ?

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

Thnx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水水月牙 2024-08-29 13:08:31

看来我必须手动完成。我的意思是首先删除关系(EntitySet):

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

然后分配“新”EntitySet:

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

我做到了这一点,但是......
我必须对数据库应用 2 SubmitChanges() 吗?
难道不是更好的方法吗?

It appears that I have to do it manually. I mean first delete the relationships (EntitySet):

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

And after that, assign the "new" EntitySet:

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

I made it work doing that but...
DO I have to apply 2 SubmitChanges() to database?
Isn't it a better way to do it?

爱,才寂寞 2024-08-29 13:08:31

这不是更容易吗?

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

Wouldn't this be easier?

user.UserActions.Clear();
user.UserActions.AddRange(GetSelectedActions());
context.SubmitChanges();
亚希 2024-08-29 13:08:31

SQL数据库关系中是否设置了级联删除?您需要这个集合和重新生成的类来解决这个问题。

In the SQL database relationships did you have cascade delete set? You need this set and the classes regenerating to resolve this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文