Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating a entity using Entity Framework 4.3 - Code First
    primarykey
    data
    text
    <p>I'm having a hard time updating entities in Entity Framework.</p> <p>The scenario: - I load a entity with new DbContext().GetById(guid) - I try to save this entity using a extension method, then using a new DbContext()</p> <p>Heres my Update method:</p> <pre><code> public virtual void Update(IEntity entityToUpdate) { var dbEntry = Context.Entry(entityToUpdate); if (dbEntry == null) return; if (Context.Entry(entityToUpdate).State == EntityState.Detached) DbSet.Attach(entityToUpdate); else { dbEntry.CurrentValues.SetValues(entityToUpdate); Context.Entry(entityToUpdate).State = EntityState.Modified; } Context.SaveChanges(); } </code></pre> <p>This is a collection of attemps by me. If I use SetValues I'm told that the entity is detached and therefor not possible, and If I use the attach I get the following error: 'An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.'</p> <p>I'm obviously doing something fundamentally wrong. Can someone please help me in the right direction?</p> <p>UPDATE:</p> <pre><code> protected void TransferClubs(object sender, EventArgs e) { var clubHelper = new ClubHelper(); var club = clubHelper.GetClub(new Guid("A009D0CD-71C4-42E8-88E2-037F059B12EE")); club.AddUser(Guid.NewGuid(), ClubRoleType.Admin); club.AddUser(Guid.NewGuid(), ClubRoleType.Admin); club.Save(); } public static bool Save(this ClubItem item) { var clubHelper = new ClubHelper(); clubHelper.AddOrUpdate(item); return true; } public ClubItem AddOrUpdate(ClubItem item) { if (item.Id == Guid.Empty) Insert(item); else Update(item); return item; } </code></pre> <p>And the Update() method you see in my original post...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload