Amanda Kitson
Amanda Kitson

Reputation: 5557

Entity Framework creating new entity with relationship to existing entity, results in attempt to create new copy of the existing entity

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.

What am I doing wrong?

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);

EDIT - AFTER MORE TESTING...

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

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".

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".

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);

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

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-- 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. 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.

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

Upvotes: 28

Views: 32559

Answers (4)

Sachin
Sachin

Reputation: 2775

This is how I did it in my case.

Its a similar case where Item contains ICollection<Attribute> .Here no update is done , adding already existing attribute to the item is needed.

First I looped through each attribute inside the item.

I had to first detach it from the local

    context.Set<Model.Attribute>().Local                                           
                     .Where(x => x.Id == attr.Id)
    .ToList().ForEach(p => context.Entry(p).State = EntityState.Detached);

Then I attached .

      context.Set<Model.Attribute>().Attach(attr);

Then I reloaded the datas to it .

      context.Entry(attr).Reload();

Upvotes: 0

Jeff Ogata
Jeff Ogata

Reputation: 57833

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.

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);
}

Upvotes: 7

Nuffin
Nuffin

Reputation: 3972

Try using this instead of the first three lines (which shouldn't be necessary at all, if the user object already knows it's role's ID and is discarded anyway):

int id = myUser.Role.ID; // Role should be NULL, if the user is actually new...
                         // could it be that you wanted to write myUser.RoleID?
Role myRole = myObjectContext.Roles.FirstOrDefault(x => x.ID == id);
myUser.Role = myRole;

Upvotes: -1

ken2k
ken2k

Reputation: 49013

Why are you creating a new instance of your Role entity if it already exists in the database?

Anyway, if you want to manually attach your new instance to the context, it should work if the ID of the attached instance exists in the database. But in your case the following lines are a bit strange:

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

You first create a new Role that has an ID that comes from an existing Role instance (myUser.Role) then you attach your new instance then finally you affect again your instance to the user it comes from. There's definitely something wrong here. If your Role already exists (and it appears to be the case here as you wrote myUser.Role.ID on the first line, so I assume), why are you creating a new instance.

Drop those 3 lines. Get your Role from the database. Then affect the Role that comes from the database to the myUser.Role property.

Upvotes: 1

Related Questions