0
votes

I have two simple DB Tables:

Persons (id, name, house)

Houses (id, street, city)

The 'house' column of the Persons table is the foreign key to specify in which house a person lives. Easy example. I created a model from the Database and now I am trying to learn how to work with the EF. I am for instance trying to attach a disconnected entity to an existing context:

    House dh = new House() { city = "Seattle", street = "St Road 1" };
    dh.Persons = new List<Person>();
    dh.Persons.Add(new Person { namen = "Andrew" });
    dh.Persons.Add(new Person { namen = "Thorsten" });

    YardEntities cx = new YardEntities();
    cx.Houses.Attach(dh);

The last line throws an Exception I do not understand:

Additional information: Attaching an entity of type 'DataAccessLayer.Person' failed because another entity of the same type already has the same primary key value.

Why is that? Andy ideas? The primary key 'id' has an auto increment and is the primary key of my tables.

Edit:

Code of my two entity classes:

public partial class Person
{
    public int id { get; set; }
    public string namen { get; set; }
    public int house { get; set; }

    public virtual House House1 { get; set; }
}

public partial class House
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public House()
    {
        this.Persons = new HashSet<Person>();
    }

    public int id { get; set; }
    public string street { get; set; }
    public string city { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Person> Persons { get; set; }
}
1
Share the code of the classes Person and House. It seems that Id is not set to autoincrement. When you don't define it in the instanciation, the Id property take the default value of ite type (0 for int). Here, your 2 persons have the same Id. - ADreNaLiNe-DJ
I only set the autoincrement in the database. The code for my two entities is generated by the model generator: - David
So the column is IDENTITY on the 2 tables ? - ADreNaLiNe-DJ
Yes my tables both have the IDENTITY flag in id. But in the generated code I cannot see anything regarding this. - David

1 Answers

0
votes

In your situation, I suggest you to test the second solution for DatabaseFirst context.

It seems to be a problem whit the definiton of the Primary Key and Entity Framework.

To have this problem of duplicate key, it's because it is no defined as identity (autoincrement) => your code shows that you don't define the Id when you add new Person or a House.

For DatabaseFirst context, you have the choice of solutions:

  • You modify your table to make the Id column Identity.
  • You define the Id property like this:

    House dh = new House() { Id = 1, city = "Seattle", street = "St Road 1" };
    dh.Persons.Add(new Person { Id = 1, namen = "Andrew" });
    dh.Persons.Add(new Person { Id = 2, namen = "Thorsten" });
    

For CodeFirst context, you have this 2 solutions:

  • CodeFluent solution:

    modelBuilder
        .Entity<Person>()
        .Property(f => f.Id)
        .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
  • DataAttribute solution:

    public class Person
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public long Id{ get; set; }
    
        // ...
    }