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

4

28

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... ! =)

Wayfarer answered 6/1, 2012 at 14:54 Comment(0)
A
7

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);
}
Aguascalientes answered 6/1, 2012 at 15:21 Comment(4)
I am trying to work this in to see if it resolves the problem. It appears this code is for a different version of EF since I don't have a "myObjectContext.ChangeTracker" option in VS 2010 IDE.Wayfarer
@AmandaMyer, it is for EF 4.2. If you are using EF 4, you could try the ObjectStateManager class.Aguascalientes
This wasn't the right answer but it started me down the track to solving it, so I am accepting this one.Wayfarer
@AmandaMyer i am also facing exactly the same problem. Can you please tell me the solution.Tierney
S
1

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.

Storms answered 6/1, 2012 at 15:10 Comment(7)
The reason for this is because I create the user object is initially created as a json string in the client front end, and is then deserialized by a webmethod which then attempts to save it. Since I didn't want to make a call to the DB to get the role info to serialized, just to deserialize it back to the db, I simple set the User.Role.ID property, and figured I would update the reference to that Role within the save method. That's what I'm trying to do above.Wayfarer
I have tried assigning it directly from the DB instead of doing the STUB and Attach method, but it still does the same thing.Wayfarer
Strange. Could you post the Role and User entities?Storms
How would you have me do that? They are part of an EF model file? Do you want a screenshot?Wayfarer
Yes, the entities that have been generated by EF. It would be interesting to see the definition of the Role property of your User entity. Go to the definition of your property (in Visual Studio, click on the property and press F12) then copy/paste it. Maybe be there is a problem with a navigation property.Storms
Here is a screenshot of the EF model with the Mapping details and Properties shown. plus.google.com/u/0/photos/110645578999953712393/albums/…Wayfarer
I did try just removing those three lines altogether and using this: 'myUser.Role = myObjectContext.Roles.FirstOrDefault(x => x.ID == myUser.Role.ID);' And that does update the Role (I can see it updated before it his the DB). But then the DB is still complaining about me trying to insert a new Role without a "Name" which is a non-nullable column. It almost seems like it may be inserting another role from somewhere else?Wayfarer
P
0

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();
Pled answered 1/4, 2018 at 14:27 Comment(0)
G
-1

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;
Goddamn answered 6/1, 2012 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.