EF复合键流利的API [英] EF Composite key fluent API

查看:179
本文介绍了EF复合键流利的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; }
}

和它的地图:

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

和它的图:

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



不过,我不断收到以下异常:

But I keep getting the following exception:

被模型生成过程中检测到一个或多个验证错误:结果
System.Data.Entity.Edm.EdmEntityType:的EntityType CustomerImage没有定义键。定义此的EntityType关键结果
System.Data.Entity.Edm.EdmEntitySet:的EntityType:EntitySet的'CustomerImages'是基于类型'CustomerImage'是没有定义的键

One or more validation errors were detected during model generation:
System.Data.Entity.Edm.EdmEntityType: : EntityType 'CustomerImage' has no key defined. Define the key for this EntityType.
System.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'CustomerImages' is based on type 'CustomerImage' that has no keys defined.

然而,如果我定义组合键与数据的注解,这是不是很漂亮,它完美的作品:

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

和它的图:

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



我试过的定义但没有许多变化似乎工作。
你知道吗?难道是EF错误

I've tried many variations of the definitions but none seems to work. Any idea? Is it EF bug?

推荐答案

事实证明,我只是忘了把对的DbContext地图:

As it turned out, I simply forgot putting the map on the DbContext:

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

这是说,复合标识仍然没有在$人口元这种方式。因此使用数据annotaion这是生成的元数据:

That said, the composite Id still not being populated on the $metadata this way. 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>



不过,如果用流利的API,而不是数据标注,未生成的关键部分都没有。
为什么?

However, if using fluent API instead of data annotation, the key part is not being generated at all. Why?

这篇关于EF复合键流利的API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆