生成模型时,许多关系会自动映射。但是,某些关系是“不正确的”(或者至少不是我想要的)或丢失。
我毫不怀疑这是由于数据库设计不佳造成的,但是基于我在该项目中的角色,我无能为力。但是,我可以在应用程序代码中做些什么来修复映射?
这是一个例子:
我想将StoreProductId属性映射到StoreProducts表。
public partial class ProductAttributePriceAdjustment
{
public int AdjustmentId { get; set; }
public int StoreProductId { get; set; }
public int StoreId { get; set; }
public string ProductId { get; set; }
public Nullable<int> ProductSizeId { get; set; }
public Nullable<decimal> Adjustment { get; set; }
public int PointsAdjustment { get; set; }
public Nullable<int> ProductColorID { get; set; }
public StoreProduct StoreProduct { get; set; }
}
public partial class StoreProduct
{
public int StoreProductID { get; set; }
public int StoreID { get; set; }
public string ProductID { get; set; }
public bool Featured { get; set; }
public bool Clearance { get; set; }
}
我认为,当我尝试调用类似以下内容的内容时:
@adjustment.StoreProduct.ProductID
我收到此错误:
Object reference not set to an instance of an object.
我遵循了Frans的建议并将我的模型更新为:
public partial class ProductAttributePriceAdjustment
{
public int AdjustmentId { get; set; }
public int StoreProductId { get; set; }
public int StoreId { get; set; }
public string ProductId { get; set; }
public Nullable<int> ProductSizeId { get; set; }
public Nullable<decimal> Adjustment { get; set; }
public int PointsAdjustment { get; set; }
public Nullable<int> ProductColorID { get; set; }
public virtual StoreProduct StoreProduct { get; set; }
}
但仍然出现相同的错误。
您不能在这样的实体框架中创建1:1映射。不支持。
实体框架仅支持1:1映射,其中两个表都具有共享的主键(即,它们具有相同的主键,并且其中一个是另一个的外键)。在您所处的情况下,实际上是在创建1对1的对象,因为不能保证StoreProductId是唯一的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句