使用相同的密钥附加分离的条目

发布于 2024-11-29 08:24:21 字数 464 浏览 7 评论 0原文

我正在使用 Code First Entity Framework 4.1。我使用的两个实体是“状态”和“用户”。每个状态条目都有一个“CreatedBy”用户和“ModifiedBy”用户属性,如下所示。

public class State {
    public virtual User CreatedBy { get; set; }
    public virtual User ModifiedBy { get; set; }
}

User 实体没有任何对 State 实体的反向引用,即 State =>用户是“单向”的。

当存在具有相同“CreatedBy”和“ModifiedBy”用户属性的分离状态实体时,就会出现问题。当我尝试将状态实体附加到 dbContext 时,EntityFramework 抱怨 ObjectStateManager 发现了重复条目。我一直在寻找解决这个问题的简单方法。

I am using Code First Entity Framework 4.1. The two entities that I am using are "State" and "User". Each State entry has a "CreatedBy" User and "ModifiedBy" User properties as given below.

public class State {
    public virtual User CreatedBy { get; set; }
    public virtual User ModifiedBy { get; set; }
}

The User entity doesn't have any back reference to State entity, that is State => User is "Unidirectional".

The problem occurs when there is a detached State entity which has same "CreatedBy" and "ModifiedBy" User properties. When I try to attach State Entity to the dbContext, the EntityFramework complains that duplicate entry found by ObjectStateManager. I was looking for a simple solution for this issue.

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

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

发布评论

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

评论(1

最单纯的乌龟 2024-12-06 08:24:21

一种解决方案是检查具有相同密钥的 User 是否已在上下文中,如果是,则替换 StateUser 引用> 实体由附加到上下文的对象组成。比如说,state 是要附加的新 State 实体:

if (state.CreatedBy != null)
{
    var attachedCreatedBy = context.ChangeTracker.Entries()
        .Where(e => e.Entity is User
                 && e.Cast<User>().Entity.Id == state.CreatedBy.Id)
        .Select(e => e.Entity)
        .SingleOrDefault();

    if (attachedCreatedBy != null)
        state.CreatedBy = attachedCreatedBy;
}

if (state.ModifiedBy != null)
{
    var attachedModifiedBy = context.ChangeTracker.Entries()
        .Where(e => e.Entity is User
                 && e.Cast<User>().Entity.Id == state.ModifiedBy.Id)
        .Select(e => e.Entity)
        .SingleOrDefault();

    if (attachedModifiedBy != null)
        state.ModifiedBy = attachedModifiedBy;
}

context.States.Attach(state); // now it should not throw an exception anymore

嗯,不过,我不会将此称为“简单的解决方案”。但我不知道另一位。如果您在 State 中有外键属性 CreatedByIdModifiedById ,事情会变得更容易。您只需将导航属性 CreatedByModifiedBy 设置为 null,并仅将外键属性设置为相关用户的 Id。

One solution would be to check if a User with the same key is already in the context and if yes, replace the detached User references in your State entity by the objects which are attached to the context. Say, state is the new State entity to attach:

if (state.CreatedBy != null)
{
    var attachedCreatedBy = context.ChangeTracker.Entries()
        .Where(e => e.Entity is User
                 && e.Cast<User>().Entity.Id == state.CreatedBy.Id)
        .Select(e => e.Entity)
        .SingleOrDefault();

    if (attachedCreatedBy != null)
        state.CreatedBy = attachedCreatedBy;
}

if (state.ModifiedBy != null)
{
    var attachedModifiedBy = context.ChangeTracker.Entries()
        .Where(e => e.Entity is User
                 && e.Cast<User>().Entity.Id == state.ModifiedBy.Id)
        .Select(e => e.Entity)
        .SingleOrDefault();

    if (attachedModifiedBy != null)
        state.ModifiedBy = attachedModifiedBy;
}

context.States.Attach(state); // now it should not throw an exception anymore

Well, I would not call this a "simple solution" though. But I don't know another one. If you had foreign key properties CreatedById and ModifiedById in State it would become easier. You could just set the navigation properties CreatedBy and ModifiedBy to null and only set the foreign key properties to the Ids of the related users.

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