EF 复合键流畅 API

本文关键字:API 复合 EF | 更新日期: 2023-09-27 18:35:38

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

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: : EntityType 'CustomerImage' 未定义键。定义此实体类型的键。
System.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet '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错误吗?

EF 复合键流畅 API

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

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

也就是说,复合 Id 仍然没有以这种方式填充在$metadata上。因此,使用数据注释,这是生成的元数据:

<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 而不是数据注释,则根本不会生成关键部分。为什么?