I have two tables that one of them relate to other. The first table
public class Text
{
[Key]
[Column(Order = 1)]
[Required]
[MinLength(7)]
[MaxLength(7)]
public string Fieldname { get; set; }
[Key]
[Column(Order = 2)]
[Required]
public virtual Language Language { get; set; }
[MaxLength(50)]
[MinLength(1)]
[Required]
public string Description { get; set; }
}
and the second table:
public class Language
{
[Key]
[Required]
[MaxLength(2), MinLength(2)]
public string Code { get; set; }
[Required]
public string Country { get; set; }
}
The seed data looks as follow:
context.Language.AddOrUpdate(
new Language() {Code = "DE", Country = "German"},
new Language() {Code = "EN", Country = "English"});
context.Text.AddOrUpdate(
new Text { Fieldname = "TEXT001", Description = "Server", Language = context.Language.First(e => e.Code == "EN") },
new Text { Fieldname = "TEXT001", Description = "Server", Language = context.Language.First(e => e.Code == "DE") }
);
I update the db and got following error message :
System.Data.Entity.Core.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint 'PK_dbo.Texts'. Cannot insert duplicate key in object 'dbo.Texts'. The duplicate key value is (TEXT001).
What is wrong?