Entity Framework 具有复合键:违反 PRIMARY KEY 约束
Entity Framework with composite key: Violation of PRIMARY KEY constraint
这是我的模型:
[Key, Column(Order=1)] // composite key with ExternalId. WebshopId+ExternalId must be unique
[ForeignKey("Webshop_Id")]
public virtual Webshop WebshopEntity
{
get { return _Webshop; }
set { _Webshop = value; }
}
[NotMapped]
private Webshop _Webshop;
public int Webshop_Id { get; set; }
[Key, Column(Order = 2)]
[MinLength(1, ErrorMessage = "ExternalId must be atleast 1 character")]
public virtual string ExternalId { get; set; }
Product
应该有 Webshop(它的父级)和 ExternalId
.
的复合主键
现在我的数据库已满,我要添加一个新的 Product
作为父级 ExternalId
但 Webshop
不同。这会导致以下错误:
Violation of PRIMARY KEY constraint 'PK_dbo.Products'. Cannot insert duplicate key in object 'dbo.Products'. The duplicate key value is (110).
我做错了什么? (使用 EF6)
[Key]
注释应该在 Webshop_Id
而不是 WebshopEntity
上定义。
这是我的模型:
[Key, Column(Order=1)] // composite key with ExternalId. WebshopId+ExternalId must be unique
[ForeignKey("Webshop_Id")]
public virtual Webshop WebshopEntity
{
get { return _Webshop; }
set { _Webshop = value; }
}
[NotMapped]
private Webshop _Webshop;
public int Webshop_Id { get; set; }
[Key, Column(Order = 2)]
[MinLength(1, ErrorMessage = "ExternalId must be atleast 1 character")]
public virtual string ExternalId { get; set; }
Product
应该有 Webshop(它的父级)和 ExternalId
.
现在我的数据库已满,我要添加一个新的 Product
作为父级 ExternalId
但 Webshop
不同。这会导致以下错误:
Violation of PRIMARY KEY constraint 'PK_dbo.Products'. Cannot insert duplicate key in object 'dbo.Products'. The duplicate key value is (110).
我做错了什么? (使用 EF6)
[Key]
注释应该在 Webshop_Id
而不是 WebshopEntity
上定义。