Magnus
Magnus

Reputation: 514

Entity framework tries to add ID explicitly when adding entry

Normally, when adding an entry with Entity Framework, it knows to ignore the ID column when it is an identity key. However, for some reason, after adding a relationship to my Company class, every time Entity Framework tries to add a new Company entry it tries to add explicitly the ID with a 0 value and fails. Also, whenever I try to add an entry of any class with a relationship to the Company class, it fails with to a constraint violation.

This is the Company class:

 public int Id { get; set; }
 [Required]
 public string Nombre { get; set; }
 [ForeignKey("Customer")]
 public int CustomerId { get; set; }
 [ForeignKey("CompanyType")]
 public string CompanyTypeCode{ get; set; }
 /* more properties */

public virtual Parameters Parameters { get; set; }

The problem began to occur when I added the Parameters virtual property to the class. This is the Parameters class

public int Id { get; set; }
public int CompanyId { get; set; }
/* more stuff */
public virtual Company Company { get; set; }

Also, I had to define the relationship with Fluent API:

modelBuilder.Entity<Company>().HasRequired(e => e.Parameters).WithRequiredDependent(p => p.Company);

This is the command Entity Framework tries to execute. Note that it tries to explicitly add the Id instead of ignoring it:

INSERT [dbo].[Company]([Id], [Name], [CustomerId], [Code], [POS], [CompanyCode], [VAT], [Path], [ExpiryDate], [CreatedBy], [CreateDate], [UpdatedBy], [UpdateDate])
VALUES (@0, @1, @2, @3, @4, @5, @6, NULL, NULL, @7, @8, @9, @10)
-- @0: '0' (Type = Int32)
-- @1: 'asdasd' (Type = String, Size = -1)
-- @2: '9' (Type = Int32)
-- @3: '20222222223' (Type = String, Size = -1)
-- @4: '   1' (Type = String, Size = -1)
-- @5: '1' (Type = String, Size = 128)
-- @6: '4' (Type = Int32)
-- @7: '24' (Type = Int32)
-- @8: '11/4/2017 17:46:22' (Type = DateTime2)
-- @9: '24' (Type = Int32)
-- @10: '11/4/2017 17:46:22' (Type = DateTime2)

Upvotes: 2

Views: 622

Answers (2)

deicode
deicode

Reputation: 63

just because at creation of your table "company" The property of the ID field that is a primary key has not been set as autoincrement. Adding a new value to your table is a violation because the value in the ID field is always 1

Upvotes: 0

Gert Arnold
Gert Arnold

Reputation: 109261

The mapping ...

modelBuilder.Entity<Company>()
    .HasRequired(e => e.Parameters)
    .WithRequiredDependent(p => p.Company);

... has turned Company into a dependent entity, which means that it no longer creates its own primary key, but it waits for Parameters to get a primary key and then takes that value for its own primary key (which is a foreign key at the same time).

I think in your case, this mechanism is hampered somewhat by Parameters.CompanyId so EF ends up inserting 0 for Company.Id. But that doesn't really matter, because you should turn it aroung: Parameters should be the dependent entity:

modelBuilder.Entity<Company>()
    .HasRequired(e => e.Parameters)
    .WithRequiredPrincipal(p => p.Company);

Now Parameters will have a primary key that's also a foreign key (to Company), making Parameters.CompanyId redundant.

By the way, using plural names for classes is confusing.

Upvotes: 1

Related Questions