【问题标题】:How to map child entity to parent entity without introducing primitive type parent linker in child entity?如何在不在子实体中引入原始类型父链接器的情况下将子实体映射到父实体?
【发布时间】:2011-08-03 06:53:14
【问题描述】:

我有这些课程:

public class Product
{
    [Key]
    public virtual int ProductId { get; set; }
    public virtual string ProductName { get; set; }
    public virtual string Category { get; set; }

    public virtual IList<ProductPricing> ProductPriceList { get; set; }


    [Timestamp]
    public virtual byte[] Version { get; set; }
}

public class ProductPricing
{        

    // no ProductId here
    public virtual Product Product { get; set; }

    [Key]
    public virtual int ProductPricingId { get; set; }

    public virtual DateTime EffectiveDate { get; set; }
    public virtual decimal Price { get; set; }


}

这是我的模型构建器:

modelBuilder.Entity<Product>().
    HasMany(x => x.ProductPriceList)
   .WithRequired()
   .HasForeignKey(x => x.Product);

这是错误:

外键组件“产品”不是在 输入“产品定价”。验证它没有被明确排除 来自模型并且它是一个有效的原始属性。

更新

我已经尝试了以下,代码下方的相应错误

modelBuilder.Entity<Product>()
    .HasMany(x => x.ProductPriceList)
    .WithRequired();

{"无效的列名 'Product_ProductId1'。\r\n无效的列名 'Product_ProductId'。\r\n无效的列名'Product_ProductId1'。"}

modelBuilder.Entity<Product>()
    .HasMany(x => x.ProductPriceList)
    .WithRequired()
    .Map(x => x.MapKey("ProductId"));

{"无效的列名'Product_ProductId'。"}

modelBuilder.Entity<Product>()
    .HasMany(x => x.ProductPriceList)
    .WithRequired(x => x.Product);

{"无效的列名 'Product_ProductId'。\r\n无效的列名 'Product_ProductId'。"}

modelBuilder.Entity<Product>()
    .HasMany(x => x.ProductPriceList)
    .WithRequired(x => x.Product)
    .Map(x => x.MapKey("ProductId"));

{"违反多重性约束。角色 'Product_ProductPriceList_Source' 的关系 'TestEfCrud.Mappers.Product_ProductPriceList' 具有多重性 1 或 0..1."}

如果有帮助,这里是 DDL:

create table Product
(
ProductId int not null identity(1,1) primary key,
ProductName varchar(100) not null,
Category varchar(100) not null,
Version rowversion not null
);

create table ProductPricing
(
ProductId int not null references Product(ProductId),
ProductPricingId int identity(1,1) not null primary key,
EffectiveDate datetime not null,
Price decimal(18,6) not null
);

更新 2

我已经尝试过这个答案,它看起来与我的情况类似 bit,映射源自子实体 How to map parent column in EF 4.1 code first

但是,使用这个:

modelBuilder.Entity<ProductPricing>()
    .HasOptional(x => x.Product)
    .WithMany()
    .Map(x => x.MapKey("ForeignKeyColumn"));

还有这个:

modelBuilder.Entity<ProductPricing>()
    .HasRequired(x => x.Product)
    .WithMany()
    .HasForeignKey(x => x.Product);

两者都导致了这个错误:

{"无效的列名 'Product_ProductId1'。\r\n无效的列名 'Product_ProductId1'。\r\n无效的列名'Product_ProductId1'。"}

【问题讨论】:

    标签: entity-framework mapping entity-framework-4.1


    【解决方案1】:

    我不明白你为什么要使用流畅的映射?您的模型应该按照默认约定进行映射。如果你想用流畅的映射来映射它:

    modelBuilder.Entity<Product>()
                .HasMany(x => x.ProductPriceList) // Product has many ProductPricings
                .WithRequired(y => y.Product)     // ProductPricing has required Product
                .Map(m => m.MapKey("ProductId")); // Map FK in database to ProductId column
    

    【讨论】:

    • 我试过不放任何模型构建器流畅的映射,期望约定优于配置(我的类是典型的新建类),但唉,它有错误。 {"Invalid column name 'Product_ProductId'.\r\nInvalid column name 'Product_ProductId'.\r\nInvalid column name 'Product_ProductId'."}
    • 你在使用现有的数据库吗?
    • 现有数据库只有一行在父(产品)上
    • 我在问题中添加信息,我发布了 DDL
    • 好的,我修改了答案,所以它应该将 FK 映射到正确的列。
    【解决方案2】:

    这是正确答案:

    http://agilenet.wordpress.com/2011/04/18/entity-framework-code-first-specify-foreign-key-name-in-one-to-many-relationship-with-fluent-api/

    我几乎明白了:

    modelBuilder.Entity<ProductPricing>()
        .HasRequired(x => x.Product)
        .WithMany()
        .Map(x => x.MapKey("ProductId"));
    

    我只是忘了把校长的家属(即ProductPriceList。我希望我得到正确的术语,想远离父子术语^_^):

    modelBuilder.Entity<ProductPricing>()
        .HasRequired(x => x.Product)
        .WithMany(x => x.ProductPriceList)
        .Map(x => x.MapKey("ProductId"));
    

    Entity Framework 的 Fluent Mapping 很难 fluent,如果您不是很熟悉每种方法的细微差别,您可能会在不知不觉中犯下一些口吃 :-) 看,我几乎说对了。同时传递ProductPricingProductPriceList 看起来是多余的,很难直观。

    EF 的 fluent 映射很难说是一个好的 fluent(直觉性应该是与生俱来的品质)界面公民,不是吗?

    【讨论】:

    • 我刚刚测试了我的映射,它完全符合您的要求。您所描述的只是关系反面的相同映射。
    猜你喜欢
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    相关资源
    最近更新 更多