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;
..
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.
I also had an issue with duplicate keys. For me the reason was that I mistakenly used
Id = new Guid()
instead of
Id = Guid.NewGuid()
in a factory method for initializing my database.
I just came across this. Like the marked answer states, TPC doesn't set the key to an identity. So you can use an annotation for it:
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
I just tested it and tables of derived types in TPC (table per concrete type) inheritance don't have PK defined as identity in EF 4.1 RC.