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;
  ... some other stuff

}

class User : Member
{
    public virtual string Name;
    ... some more props no new key defined
}

class MyDbContext
{
    public DbSet<User> Users {get;set;}
}

The result of the deployment to SQL Server is just ok, i've a Members Table and a Users Table (TPC). Now in my main i've the following code:

static Main()
{
   User x,y;
   using(MyDbContext ctx = new MyDbContext())
   {


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

   }
}

On save changes i get:

"Unhandled Exception: System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for de tails. ---> System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient .SqlException: Violation of PRIMARY KEY constraint 'PK_Users_0CF04B1821B6055D'. Cannot insert duplicate key in object 'dbo.Users'."

The MemberId by default is identity so i just not seeing what's going on here. i've tried to create the instances with new and Attach them to the context but the error was the same, any ideas? The database is empty!

Thanks.

EDIT: Forgot to told that Member is an abstract class sorry bout that.

link|improve this question

73% accept rate
feedback

2 Answers

up vote 1 down vote accepted

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.

link|improve this answer
Thats true, but the Member table have an identity PK so it should do the binding automatically – DVD Mar 27 '11 at 19:07
No it should not because it is table per concrete type. Each table handles its own PK. Moreover identity columns should not be used in TPC inheritance because if you query all members it will return users as well. If you do not change seeds in identity you will easily get users and members with the same id. I expect exception in that case. – Ladislav Mrnka Mar 27 '11 at 19:10
It makes sense, the thing is that i have some common info (Member) and some other specific info (e.g User, Admin, Guest) i've mapped the member to a table because if i want to know some member name i dont need to know if it is a User or Admin, etc. The point is that an User is a Member directly so it is impossible to have the same id... Member is also an abstract class so i can't even create instances. . . – DVD Mar 27 '11 at 19:16
Do you really need inheritance for this. Or better do you really need TPC inheritance? Isn't TPH enough (all members in single table and discriminator column differing type of member)? – Ladislav Mrnka Mar 27 '11 at 19:19
No TPC was the best because 90% of time i dont need to load de User information, thats why – DVD Mar 27 '11 at 19:27
show 2 more comments
feedback
  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.

link|improve this answer
No it doest work, the property MemberId is identity so it should not be in the Insert Query. . . The id is always 0 that's the problem, but when I Submit the changes the EF should change the proxys MemberId automatically – DVD Mar 27 '11 at 15:15
You should try to attach your entity before you modify it. – Jean-Philippe Leclerc Mar 27 '11 at 15:16
It gives other exception with the same problem, "an object with the same key already exists"... – DVD Mar 27 '11 at 15:20
What happen if you set the entityKey to null before you save? – Jean-Philippe Leclerc Mar 27 '11 at 15:23
it's an integer ... – DVD Mar 27 '11 at 15:46
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.