tengo estas clases:Cómo mapear la entidad hijo a la entidad principal sin introducir el vinculador principal de tipo primitivo en la entidad secundaria?
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; }
}
Este es mi modelBuilder:
modelBuilder.Entity<Product>().
HasMany(x => x.ProductPriceList)
.WithRequired()
.HasForeignKey(x => x.Product);
Este es el error:
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.
ACTUALIZACIÓN
he probado el siguiente, error correspondiente ORS por debajo del código de
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."}
Si se pudiera ayudar, aquí está el 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
);
ACTUALIZACIÓN 2
He tratado de esta respuesta, que parece un poco similar a mi caso, el mapeo se originó a partir entidad secundaria How to map parent column in EF 4.1 code first
Sin embargo, el uso de este:
modelBuilder.Entity<ProductPricing>()
.HasOptional(x => x.Product)
.WithMany()
.Map(x => x.MapKey("ForeignKeyColumn"));
y esto:
modelBuilder.Entity<ProductPricing>()
.HasRequired(x => x.Product)
.WithMany()
.HasForeignKey(x => x.Product);
Ambas resultaron de este error:
{"Invalid column name 'Product_ProductId1'.\r\nInvalid column name 'Product_ProductId1'.\r\nInvalid column name 'Product_ProductId1'."}
He intentado no poner cualquier mapeo fluido de modelBuilder, esperando que la convención sobre configuración se active (mis clases son típicas de las nuevas), pero lamentablemente tiene un error. '{" Nombre de columna no válido 'Product_ProductId'. \ R \ nNombre de columna no válido 'Product_ProductId'. \ R \ nNombre de columna no válido 'Product_ProductId'. "}' – Hao
¿Está utilizando una base de datos existente? –
Base de datos existente con una sola fila en el documento principal (Producto) – Hao