我收到消息... *列名“PartLot_Id”无效*
我想它指的是数据库中 PART_LOT 的 ID 字段。显然,这张表,没有其他人(不太清楚,但相信我)拥有一个字段“PartLot_Id”,所以从表面上看这是有道理的。
当我为这个表编写相应的实体时,我想让它更友好一点,所以我改变了表和字段的大小写,并且在某些情况下完全重命名了字段(试图区分业务关注点和数据)。我做到了,但是用适当的属性装饰类和属性,这些属性应该向 EF 发出信号,表明 DataStore 中的实际名称是什么。到目前为止,这对我所有的实体都有效。
[Table("PART_LOT")]
public partial class PartLot : ModelBase
{
[Key]
[Column("ID")]
public Int32 Id { get; set; }
[Column("LOT_IDENT")]
public String LotIdentity { get; set; }
[Column("PART_ID")]
public Guid? PartId { get; set; }
[Column("COMPANYSITE_ID")]
public Guid? CompanySiteId { get; set; }
#region Navigation Properties
[ForeignKey("PartId")]
public virtual Part Part { get; set; }
[ForeignKey("CompanySiteId")]
public virtual Company Company { get; set; }
public virtual ICollection<StrategicPart> StrategicParts { get; set; }
public virtual ICollection<Product> Products{ get; set; }
#endregion
}
似乎 EF 忽略了这些属性并实现了它的约定,据我所知,假设 Key 字段名称是实体名称加上“Id”。
谁能解释为什么这些属性似乎被忽略了?
更新
@kirtsen g- 感谢您的回复。我觉得我们可能在正确的轨道上,或者比我现在更近的地方。我正在使用我最初排除的一些附加信息来更新 OP,以保持帖子的清洁和尽可能整洁。
PartLot 被另一个实体模型上的导航属性引用,但它也被正确注释(?)
[Table("STRATEGIC_PART")]
public partial class StrategicPart : ModelBase
{
[Key]
[Column("ID")]
public Int64 Id { get; set; }
[Column("PRODUCT_ID")]
public Guid ProductId { get; set; }
[Column("PART_LOT_ID")]
public Int32 PartLotId { get; set; }
#region Navigation Properties
[ForeignKey("ProductId")]
public virtual Product Product { get; set; }
[ForeignKey("PartLotId")]
public virtual PartLot Lot { get; set; }
#endregion
}
StrategicPart 模型的“Lot”属性返回一个“PartLot”实体(我将名称更改为简单的“Lot”,因为 StrategicPart.PartLot 似乎是多余的),但我确实将 ForeignKeyAttribute 分配给“PartLotId”以尝试覆盖任何CodeFirst 假设/约定(我对配置模型的约定的问题之一)。
你知道,它只是发生在我身上,我不确定这是否可能很重要,但是 StrategicPart 实体,因此数据库中的 STRATEGIC_PART 表,实际上是一个多对多关系的连接产品和零件批次。
再次感谢!
更新
@kirsten_g - 谢谢你和我在一起!!我已按要求添加了 Product 类。
[Table("PRODUCT")]
public partial class Product : ModelBase
{
[Key]
[Column("ID")]
public Guid Id { get; set; }
[Column("MFG_INFO_ID")]
public Guid? ManufacturerInfoId { get; set; }
[Column("MODEL_ID")]
public Guid ModelId { get; set; }
[Column("MODEL_CODE")]
public String ModelCode { get; set; }
[Column("CONFIG_CODE")]
public String ConfigCode { get; set; }
[Column("SERIAL_NUMBER")]
public String SerialNumber { get; set; }
[Column("FULL_SN")]
public String FullSerialNumber { get; set; }
[Column("SW_VERSION")]
public String SoftwareVersion { get; set; }
[Column("REWORKED")]
public Boolean IsReworked { get; set; }
[Column("DATAFILE_ID")]
public Int32 DatafileId { get; set; }
[Column("SILICON_ID")]
public Guid? SiliconId { get; set; }
[Column("IS_PART_EXCEPTION_CALCULATED")]
public Boolean? IsPartExceptionCalculated { get; set; }
[Column("STATUS")]
public String Status { get; set; }
[Column("STATUS_CHANGED_DT")]
public DateTime StatusChangeDate { get; set; }
#region Navigation Properties
[ForeignKey("ModelId")]
public virtual ProductModel Model { get; set; }
#endregion
}
更新:解决方案
感谢 kirsten_g,我找到了问题所在。通过要求查看 Product 类,我突然想到我没有在其中添加对 STRATEGIC_PART (StrategicPart) 实体的引用。当我添加它时,它并没有帮助,但后来我记得...... STRATEGIC_PART 的唯一目的是促进多对多连接。
如果我让 EF 自己创建模型,它就不会为加入实体的导航属性而烦恼。所以我手动做了同样的事情。忽略StrategicPart 实体,我将两个实体中的导航属性直接添加到彼此,并删除了任何引用StrategicPart 的导航属性。所以更新后的 Product 和 PartLot 类看起来像......
[Table("PRODUCT")]
public partial class Product : ModelBase
{
[Key]
[Column("ID")]
public Guid Id { get; set; }
// Removed properties for clarity. Still contatins all the properties defined above.
#region Navigation Properties
[ForeignKey("ModelId")]
public virtual ProductModel Model { get; set; }
// Added Nav Property to PartLots
public virtual ICollection<PartLot> PartLots{ get; set; }
#endregion
}
[Table("PART_LOT")]
public partial class PartLot : ModelBase
{
[Key]
[Column("ID")]
public Int32 Id { get; set; }
// Removed properties for clarity. Still contatins all the properties defined above.
#region Navigation Properties
[ForeignKey("PartId")]
public virtual Part Part { get; set; }
[ForeignKey("CompanySiteId")]
public virtual Company Company { get; set; }
// Remove Nav Property to StrategicPart
// public virtual ICollection<StrategicPart> StrategicParts { get; set; }
public virtual ICollection<Product> Products{ get; set; }
#endregion
}
现在我的实体正确地相互引用,我的错误已经消失了!我已将 Kirsten_g 的答案标记为带有上述扩展名的答案!
谢谢大家帮忙。我希望这对其他人也有帮助。