我對實體框架比較陌生,所以我可能會在這裏丟失一些明顯的東西,但我試圖使用一些現有的數據庫體系結構來創建新的應用程序。我不確定你在這裏需要多少信息,所以我會盡可能多地放。實體框架無效的列名'Model_Key'
我有三個模型,我試圖設置它們,使它們都是相互關聯的。
LocatorTier
[Table("locator_tag_definitions")]
public class LocatorTier
{
public enum TierClassification
{
Tier1 = 16,
Tier2 = 17,
Tier3 = 18
}
[Key]
[Column("TagID")]
public int LocatorTagID { get; set; }
[Column("Name")]
public string SectionName { get; set; }
[Column("ClassificationTagID")]
public TierClassification tierClassification { get; set; }
public int ParentID { get; set; }
public int LocatorID { get; set; }
public List<LocatorAttribute> LocatorAttributes { get; set; }
}
LocatorAttribute
[Table("locator_tags_attributes")]
public class LocatorAttribute
{
[Column("ID")]
public int LocatorAttributeID { get; set; }
public int LocatorID { get; set; }
[Column("TagID")]
public int LocatorTagID { get; set; }
public int CatalogueID { get; set; }
[ForeignKey("LocatorAttributeDefinitions")]
public int AttributeDefID { get; set; }
public List<LocatorAttributeValue> LocatorAttributeValues { get; set; }
}
LocatorAttributeValue
[Table("assets_attribute_values")]
public class LocatorAttributeValue
{
[Key]
public int ValueID { get; set; }
public int AttributeDefID { get; set; }
public int AssetID { get; set; }
public string ShortValue { get; set; }
}
我在我的控制器用下面的代碼調用此:
LocatorTier locatorTier = StoreDB.LocatorTier.Include(lt => lt.LocatorAttributes.Select(lta => lta.LocatorAttributeValues)).Single(i => i.LocatorID == 940 && i.tierClassification == LocatorTier.TierClassification.Tier1);
一切正常,如果我不嘗試在LocatorAttributeValue帶來,但是當我做我出現以下錯誤:
Invalid column name 'LocatorAttribute_LocatorAttributeID'.
我相信這是因爲我的表「assets_attribute_values」沒有一個叫「LocatorAttributeID」一欄。不過,我期待這一點,並試圖設置關係,以便兩個表通過「AttributeDefID」列鏈接;
什麼我錯在這裏做什麼,有什麼辦法可以在不修改數據庫中的表了鏈接這些?
當我看到這條消息之前,通常是因爲我錯過了一個EntityTypeConfiguration文件從modelBuilder.Configuations – Slicksim 2014-10-29 22:18:41