如何将子实体映射子实体到父实体,而不在子实体中引入原语类型父链接器? [英] How to map child entity to parent entity without introducing primitive type parent linker in child entity?

查看:30
本文介绍了如何将子实体映射子实体到父实体,而不在子实体中引入原语类型父链接器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类:

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:

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

这是错误:

外键组件"Product"不是上声明的属性 键入"ProductPricing"。验证它是否未被明确排除 并且它是有效的基元属性。

更新

我尝试了以下方法,代码下面出现了相应的错误

modelBuilder.Entity<Product>()
    .HasMany(x => x.ProductPriceList)
    .WithRequired();
{"列名‘Product_ProductId1’无效。 无效的列名 "Product_ProductID"。 列名‘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’无效。 无效的列名 ‘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

我已经尝试过这个答案,它看起来与我的情况相似,映射起始子实体 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’无效。 无效的列名 "Product_ProductId1"。 列名‘Product_ProductId1’无效。"}

推荐答案

我不明白您为什么要使用流畅映射?您的模型应该按照默认约定进行映射。如果要使用流畅贴图对其进行贴图,请使用:

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

这篇关于如何将子实体映射子实体到父实体,而不在子实体中引入原语类型父链接器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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