附加具有关联的实体以进行修改

发布于 2024-10-20 20:22:01 字数 228 浏览 7 评论 0原文

我将修改具有关联集合的分离实体,例如:

人员和地址是 POCO。

当我附加实体并保存更改时,未检测到集合更改,如何更新人员的地址(添加和删除的项目)?

我需要手动跟踪我的收藏吗?

编辑

分离 POCO 的同步必须手动... EF 无意合并解决方案集合(导航属性和关系):(

我比较当前集合和原始集合,并检测差异

I would modify detached entity with associated collection, ex :

Person and Address are POCO.

When I attach entity and save changes, the collection changes isn't detected, how can I update Person with Address (added and deleted items) ?

Do I to track my collection manually ?

Edit

The synchronization of detached POCO must be manual... EF doesn't purpose merge solution of collection (navigation properties and relations) :(

I compare the current and original collections and I detect the differences

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

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

发布评论

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

评论(2

安稳善良 2024-10-27 20:22:01

如果您使用的是实体框架(我假设您使用的是实体框架,因为您将其列为问题上的标签),那么对象仅在实体上下文生成时跟踪它们的更改。

User someUser = dbEntities.Users.Single(x => x.Username == "test");
someUser.Name = "changed name";
db.SaveChanges();

该代码将检测更改并保留它们。

User someUser = new User()
{
    Username = "test" //assuming there is already user called test in the database.
}

以这种方式创建用户将不允许 EF 上下文检测到更改。相反,您需要从数据库加载实体,更新它,然后保留更改。

string username = "test";
User someUser = db.Users.Single(x => x.Username == username);
TryUpdateModel(someUser, valueProvider); //valueProvider is usually a form collection of some sort, but could be anything that implements IValueProvider.
db.SaveChanges();

这将允许您拉入实体、更新它并保存更改。

If you are using Entity Framework, which I assume you are since you listed it as a tag on your question, then objects only track their changes when they are generated by the entity context.

User someUser = dbEntities.Users.Single(x => x.Username == "test");
someUser.Name = "changed name";
db.SaveChanges();

That code will detect changes and persist them.

User someUser = new User()
{
    Username = "test" //assuming there is already user called test in the database.
}

Creating the user this way will not allow the EF context to detect the changes. Instead you will need to load the entity from the database, update it, and then persist the changes.

string username = "test";
User someUser = db.Users.Single(x => x.Username == username);
TryUpdateModel(someUser, valueProvider); //valueProvider is usually a form collection of some sort, but could be anything that implements IValueProvider.
db.SaveChanges();

This will allow you to pull in an entity, update it, and save the changes.

甜柠檬 2024-10-27 20:22:01

您还可以使用分离的 POCO 类,然后将其重新附加到上下文并将状态设置为已修改:

阅读本文:http://blogs.msdn.com/ b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx

You can also work with detached POCO class, and then re-attach it to the context and set the state to modified:

Read this article: http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx

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