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

前端 未结 4 1511
被撕碎了的回忆
被撕碎了的回忆 2021-01-25 16:46

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:07

    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.

提交回复
热议问题