EF复合主键fluent API

5

我正在尝试为一个实体映射一个复合键。

public class Customer 
{
    public int CustomerId { get; set; }
    public virtual List<CustomerImage> CustomerImages { get; set; }
}

并且它的地图:

public class CustomerMap : EntityTypeConfiguration<Customer>
{
    public CustomerMap()
    {
        HasKey(t => t.CustomerId);
        ToTable(DbConstants.k_CustomersImageTable);
    }
}

一张图片:
public class Image
{
    public int ImageId { get; set; }
}

以及它的地图:

public class ImageMap : EntityTypeConfiguration<Image>
{
    public ImageMap()
    {
        HasKey(t => t.ImageId);
        ToTable(DbConstants.k_ImagesTable);
    }
}

还有导航属性:

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; }
}

以及它的地图:

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);
    }
}

但我一直在遇到以下异常:
一个或多个验证错误在模型生成过程中被检测到: System.Data.Entity.Edm.EdmEntityType: : 实体类型 'CustomerImage' 未定义主键。请为该实体类型定义主键。 System.Data.Entity.Edm.EdmEntitySet: 实体集“CustomerImages”基于未定义主键的类型“CustomerImage”。
然而,如果我使用数据注释定义复合主键,虽然这不是很好,但它完美地解决了问题:
public class CustomerImage
{
    [Key, Column(Order = 0)]
    public int CustomerId { get; set; }
    [Key, Column(Order = 1)]  
    public int ImageId { get; set; }
}

同时附上它的地图:

public class CustomerImageMap : EntityTypeConfiguration<CustomerImage>
{
    public CustomerImageMap()
    {
        ToTable(DbConstants.k_CustomersImageTable);
    }
}

我尝试了许多定义的变化,但似乎没有一个有效。有什么想法?这是EF的错误吗?
1个回答

10

事实证明,我只是忘记将地图放在DbContext上:

modelBuilder.Configurations.Add(new CustomerImageMap());

话虽如此,这种方式仍然不能在$metadata中填充复合ID。因此,使用数据注释生成的元数据如下:

<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>

然而,如果使用流畅的API而不是数据注释,关键部分根本没有被生成。为什么呢?


1
你真的帮了我大忙。否则我可能会整天盯着这个问题。 - FatAlbert

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接