实体框架创建与现有实体有关系的新实体,导致尝试创建现有实体的新副本 [英] Entity Framework creating new entity with relationship to existing entity, results in attempt to create new copy of the existing entity

查看:78
本文介绍了实体框架创建与现有实体有关系的新实体,导致尝试创建现有实体的新副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建具有特定角色的新用户对象。 角色是EF中的现有实体。我已经用谷歌搜索了,然后堆栈溢出,直到脸色变蓝为止,并且我尝试了所有似乎对其他人都有用的东西。但是,当我尝试保存我的新用户对象时,它首先尝试创建一个新的角色,而不是仅引用现有角色创建新用户对象。

I am trying to create a new user object with a specific Role. The "Role" is an existing entity in EF. I have googled, and stackoverflowed until I am blue in the face, and I have tried all the stuff that seems to be working for everyone else. But when I try to save my new user object, it first tries to create a new "Role", instead of just creating the new user object with a reference to the existing Role.

我在做什么错了?

Role myRole = new Role { ID = myUser.Role.ID };
myObjectContext.Roles.Attach(myRole);
myUser.Role = myRole;

if (myUser.ID == 0)
{
    myObjectContext.Users.AddObject(myUser);
}
else
{
    if (myUser.EntityState == System.Data.EntityState.Detached)
    {
        myObjectContext.Users.Attach(myUser);
    }
    myObjectContext.ObjectStateManager.ChangeObjectState(myUser, System.Data.EntityState.Modified);
}
myObjectContext.SaveChanges(SaveOptions.None);

编辑-经过更多测试后...

好..所以我还是发现了原因的某些部分。我仍然不知道为什么要这样做,并且需要帮助。

Ok.. so I have discovered some portion of the "cause" anyway. I still don't know why it does this and need help.

基本上,我将两套数据附加到新的User对象上。一个是角色,它是包含角色的角色表的FK。

Basically, there are two sets of data I am attaching to my new User object. One is the "Role" which is a FK to a Role table that contains the Role. This shows up as a navigation property on the User like "User.Role".

第二组数据是一个称为 FIPS的对象的集合,它们是一个导航属性。用户和另一个称为FIPS的表之间的多对多关系。它们之间有一个关系表,其中仅包含两列,每列分别是User和FIPS的外键。用户的FIPS也是一个导航属性,其引用方式类似于 User.FIPS。

The second set of data is a collection of objects called "FIPS", which are a many-to-many relationship between the User and another table called FIPS. There is a relationship table between them, that simply contains two columns, each a foreign key to User and FIPS, respectively. The FIPS for a user are also a navigation property that is referenced like "User.FIPS".

以下是整个代码,显示了FIPS和Role的分配。保存上下文之前的用户对象。

Here is the whole code showing the assignment of the FIPS and Role to the User object prior to saving the context.

List<string> fipsList = new List<string>();
foreach (FIPS fips in myUser.FIPS)
{
    fipsList.Add(fips.FIPS_Code);
}
myUser.FIPS.Clear();
foreach (string fipsCode in fipsList)
{
    FIPS myFIPS = new FIPS { FIPS_Code = fipsCode };
    myObjectContext.FIPSCodes.Attach(myFIPS);
    myUser.FIPS.Add(myFIPS);
}


Role myRole = new Role { ID = myUser.Role.ID };
myObjectContext.Roles.Attach(myRole);
myUser.Role = myRole;


if (myUser.ID == 0)
{
   myObjectContext.Users.AddObject(myUser);
}
else
{
   if (myUser.EntityState == System.Data.EntityState.Detached)
   {
       myObjectContext.Users.Attach(myUser);
   }
   myObjectContext.ObjectStateManager.ChangeObjectState(myUser, System.Data.EntityState.Modified);
}

myObjectContext.SaveChanges(SaveOptions.None);

我设置了手表以检查 myObjectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Added)的状态

I set up my watch to check the status of "myObjectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Added)" to see when things were being added to this.

将第一个Related对象添加到User对象后,第二个Related对象尚未添加

As soon as the first Related object is added to the User object, the second Related object that hasn't yet been attached to the context, is added to the context with an EntityState of "Added".

...。将查看是否存在避免附加相关实体的方法

.... Gonna see if there is a way to avoid attaching the related entities to the User entity until after they have all been attached to the context.

-FOLLOWUP-
好​​。我更改了代码顺序,以便在将相关实体分配给用户实体之前将其关联到上下文。.但是,一旦分配了第一个相关实体,第二个相关实体在ObjectStateEntries中显示为添加 。
因此,我将其更改为以下顺序:

--FOLLOWUP-- Ok.. well I changed the order of the code so that the related entities were attached to the context before being assigned to the User entity.. but as soon as the first related entity is assigned, the second related entity is shown as "added" in the ObjectStateEntries. So, then I changed it to the following order:


  1. 将所有相关实体附加到上下文。

  2. 将用户对象上的现有关系删除为相关实体
    类型。

  3. 将相关实体分配给用户实体。

  4. 保存用户实体。

  1. Attach all related entities to context.
  2. Remove existing relationships on the user object to related entity types.
  3. Assign related entities to user entity.
  4. Save user entity.

然后..现在..它可以正常工作.. omg它可以正常工作...! =)

And.. now.. it works.. omg it works... ! =)

推荐答案

自从我编写下面的代码以来已经有一段时间了,但是我隐约记得遇到了同样的问题,之所以会发生这种情况,是因为上下文当前正在跟踪要添加的角色,因此附加存根具有添加具有相同ID的新角色的作用。

It's been a while since I wrote the code below, but I vaguely recall running into the same problem and it was occurring because the role being added was currently being tracked by the context, so attaching the stub has the effect of adding a new role with the same Id.

在以下代码中,我首先检查 ChangeTracker 并使用现有条目(如果正在跟踪角色)。

In the following code, I check the ChangeTracker first and use an existing entry if the role is being tracked.

// add roles that are in dto.Roles, but not in resource.Roles
// use the change tracker entry, or add a stub role
var rolesToAdd = fromDto.Roles.Where(r => !toResource.Roles.Any(role => role.Id == r)).ToList();
var roleEntries = dbContext.ChangeTracker.Entries<Role>();

foreach (var id in rolesToAdd)
{
    var role = roleEntries.Where(e => e.Entity.Id == id).Select(e => e.Entity).FirstOrDefault();

    if (role == null)
    {
        role = new Role { Id = id };
        dbContext.Set<Role>().Attach(role);
    }

    toResource.Roles.Add(role);
}

这篇关于实体框架创建与现有实体有关系的新实体,导致尝试创建现有实体的新副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆