Entity Framework Code First - Cannot insert duplicate key in object 'db'

前端 未结 4 912
感动是毒
感动是毒 2021-01-25 17:00

I\'m using the new EF code first to one project of mine, and i\'m getting a weird error, my mode is:

abstract class Member
{
   public virtual int MemberId;
  ..         


        
4条回答
  •  梦谈多话
    2021-01-25 17:11

    1. Is MemberId autoIncremented?
    2. Have you checked the id of your created users (with console.Writeline for exemple)?
    3. Does it work if you do the following:

      x = ctx.Users.Create();        
      x.Name = "SomeUser";        
      ctx.SaveChanges();        
      y = ctx.Users.Create();        
      y.Name = "SomeUser2";        
      ctx.SaveChanges();
      

    You should try to attach your entity before you modify it.

提交回复
热议问题