gpt4 book ai didi

entity-framework - 如何将子实体映射到父实体而不在子实体中引入原始类型父链接器?

转载 作者:行者123 更新时间:2023-12-04 18:50:23 25 4
gpt4 key购买 nike

我有这些类(class):

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

这是错误:

The foreign key component 'Product' is not a declared property on type 'ProductPricing'. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property.



更新

我已经尝试了以下代码下面的相应错误
modelBuilder.Entity<Product>()
.HasMany(x => x.ProductPriceList)
.WithRequired();

{"Invalid column name 'Product_ProductId1'.\r\nInvalid column name 'Product_ProductId'.\r\nInvalid column name 'Product_ProductId1'."}


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

{"Invalid column name 'Product_ProductId'."}


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

{"Invalid column name 'Product_ProductId'.\r\nInvalid column name 'Product_ProductId'."}


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

{"Multiplicity constraint violated. The role 'Product_ProductPriceList_Source' of the relationship 'TestEfCrud.Mappers.Product_ProductPriceList' has multiplicity 1 or 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);

两者都导致了这个错误:

{"Invalid column name 'Product_ProductId1'.\r\nInvalid column name 'Product_ProductId1'.\r\nInvalid column name '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

关于entity-framework - 如何将子实体映射到父实体而不在子实体中引入原始类型父链接器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6922690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com