[英]EF Composite key fluent API
I am trying to map a composite key for an entity. 我正在尝试为实体映射复合键。
public class Customer
{
public int CustomerId { get; set; }
public virtual List<CustomerImage> CustomerImages { get; set; }
}
And its Map: 它的地图:
public class CustomerMap : EntityTypeConfiguration<Customer>
{
public CustomerMap()
{
HasKey(t => t.CustomerId);
ToTable(DbConstants.k_CustomersImageTable);
}
}
An Image: 一个图像:
public class Image
{
public int ImageId { get; set; }
}
And its map: 它的地图:
public class ImageMap : EntityTypeConfiguration<Image>
{
public ImageMap()
{
HasKey(t => t.ImageId);
ToTable(DbConstants.k_ImagesTable);
}
}
And the navigation property: 和导航属性:
public class CustomerImage
{
public int CustomerId { get; set; }
public int ImageId { get; set; }
public virtual Customer CustomerRelated { get; set; }
public virtual Image ImageRelated { get; set; }
}
And its map: 它的地图:
public class CustomerImageMap : EntityTypeConfiguration<CustomerImage>
{
public CustomerImageMap()
{
HasKey(t => new { t.CustomerId, t.ImageId });
Property(t => t.CustomerId).IsRequired().HasColumnOrder(0);
Property(t => t.ImageId).IsRequired().HasColumnOrder(1);
HasRequired(t => t.ImageRelated).WithMany().HasForeignKey(x => x.ImageId);
HasRequired(p => p.CustomerRelated)
.WithMany(p => p.CustomerImages)
.WillCascadeOnDelete(false);
ToTable(DbConstants.k_CustomersImageTable);
}
}
But I keep getting the following exception: 但我一直得到以下例外:
One or more validation errors were detected during model generation:
在模型生成期间检测到一个或多个验证错误:
System.Data.Entity.Edm.EdmEntityType: : EntityType 'CustomerImage' has no key defined.System.Data.Entity.Edm.EdmEntityType :: EntityType'CustomerImage'没有定义键。 Define the key for this EntityType.
定义此EntityType的键。
System.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'CustomerImages' is based on type 'CustomerImage' that has no keys defined.System.Data.Entity.Edm.EdmEntitySet:EntityType:EntitySet'CustomerImages'基于类型'CustomerImage',没有定义键。
However , if I define the composite key with data annotations, which is not very nice, it works perfectly: 但是 ,如果我使用数据注释定义复合键,这不是很好,它可以完美地工作:
public class CustomerImage
{
[Key, Column(Order = 0)]
public int CustomerId { get; set; }
[Key, Column(Order = 1)]
public int ImageId { get; set; }
}
And its map: 它的地图:
public class CustomerImageMap : EntityTypeConfiguration<CustomerImage>
{
public CustomerImageMap()
{
ToTable(DbConstants.k_CustomersImageTable);
}
}
I've tried many variations of the definitions but none seems to work. 我尝试了许多定义的变化,但似乎都没有。 Any idea?
任何的想法? Is it EF bug?
是EF虫子吗?
As it turned out, I simply forgot putting the map on the DbContext: 事实证明,我只是忘了将地图放在DbContext上:
modelBuilder.Configurations.Add(new CustomerImageMap());
That said, the composite Id still not being populated on the $metadata this way. 也就是说,复合ID仍未以这种方式填充$元数据。 And so using data annotaion this is the metadata that is generated:
因此,使用数据注释,这是生成的元数据:
<EntityType Name="CustomerImage">
<Key>
<PropertyRef Name="CustomerId"/>
<PropertyRef Name="ImageId"/>
</Key>
<Property Name="CustomerId" Type="Edm.Int32" Nullable="false"/>
<Property Name="ImageId" Type="Edm.Int32" Nullable="false"/>
<Property Name="LastUpdated" Type="Edm.DateTime"/>
<NavigationProperty Name="Customer" Relationship="EasyBizy.Entities.Models.EasyBizy_Entities_Models_CustomerImage_Customer_EasyBizy_Entities_Models_Customer_CustomerPartner" ToRole="Customer" FromRole="CustomerPartner"/>
<NavigationProperty Name="Image" Relationship="EasyBizy.Entities.Models.EasyBizy_Entities_Models_CustomerImage_Image_EasyBizy_Entities_Models_Image_ImagePartner" ToRole="Image" FromRole="ImagePartner"/>
</EntityType>
However, if using fluent API instead of data annotation, the key part is not being generated at all. 但是,如果使用流畅的API而不是数据注释,则根本不会生成关键部分。 Why?
为什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.