【发布时间】:2013-08-14 22:00:36
【问题描述】:
我收到消息... *无效的列名 '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 分配给试图覆盖任何 CodeFirst 假设/约定的“PartLotId”(我的问题之一是约定优于配置模型)。
你知道,它只是发生在我身上,我不确定这是否可能重要,但 StrategicPart 实体,因此数据库中的 STRATEGIC_PART 表,实际上是多对 - Products 和 PartLos 之间有很多关系。
再次感谢!
更新
@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 的答案标记为带有上述扩展名的答案!
感谢大家的帮助。我希望这对其他人也有帮助。
【问题讨论】:
-
PartLot是一个部分类。您是否看到该类在其他地方是否有进一步的定义?另外,什么是模型库? -
@P.Brian.Mackey - 感谢您的反馈。 PartLot 被编写为部分类,主要是为了将来的可扩展性,但它目前是 PartLot 的唯一定义。 ModelBase 是一个基类,它定义了大多数实体的通用属性(LastUpdatedDate、LastUPdateBy 等)。 ModelBase 中没有对 PartLot 的引用。
标签: c# asp.net .net entity-framework