我有一个实体,该实体使用 FK ProductId 建立关系,然后我使用复合键 ProductId 和 VehicleId 在同一实体上建立另一个关系。这是行不通的。我明白了
One or more validation errors were detected during model generation:
ProductId: Name: Each property name in a type must be unique. Property name 'ProductId' is already defined.
配置代码
public class BookingConfiguration : EntityTypeConfiguration<Booking>
{
public BookingConfiguration()
{
...
HasRequired(b => b.Product)
.WithMany(p => p.Bookings)
.Map(m =>
{
m.MapKey("ProductId");
});
HasRequired(b => b.Vehicle)
.WithMany(v => v.Bookings)
.Map(m =>
{
m.MapKey("ProductId","VehicleId");
});
}
}
最佳答案
Steve Greene 让我走上正轨,我将 ProductId
和 VehicleId
添加到实体并使用
HasRequired(b => b.Vehicle)
.WithMany()
.HasForeignKey(b => new {b.ProductId, b.VehicleId});
从域的角度来看,我不喜欢向实体添加外键,因此如果有人有更好的解决方案,请告诉我。
关于c# - 属性名称 'ProductId' 已定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32523285/