2017-08-14 22 views
0

我對EF非常陌生(使用Core作爲第一個),並且一直在與一個問題作鬥爭。使用包含在Entity Framework Core中檢索字段名稱與主鍵不匹配的相關數據

簡而言之,我有兩個實體在一對一的基礎上相互關聯。在一個例子,它工作得很好,我有相關的「孩子」實體的「父」實體,如下面

[Table("Attribute", Schema = "dbo")] 
public class Attribute 
{ 
    public Attribute() 
    { 
    } 
    [Key, Column(Order = 1)] 
    public Guid EntityColumnRefNo {get; set;} 
    public string PhysicalName {get; set;} 
    public string Caption {get; set;} 
    public AttributeType AttributeType {get; set;} 
} 

[Table("AttributeType", Schema = "dbo")] 
public class AttributeType 
{ 

    public AttributeType() 
    { 
    } 
    [Key, Column(Order = 1)] 
    public Guid AttributeTypeRefNo {get; set;} 
    public string UserRefCode {get; set;} 
    public string Description {get; set;} 
} 

在數據庫中AttributeTypeRefNo在屬性表中的物理列名正是 - AttributeTypeRefNo(不知道這是否有相關性)。使用以下代碼返回屬性模型時

var res = db?.Attribute 
    .Where(x => x.EntityRefNo == entityrefNo) 
    .Include(c => c.AttributeType) 

它工作得很好。該屬性返回與填充的AttributeType。

當在屬性上方添加另一個模型並且引用屬性兩次時,會出現我的問題。

[Table("Relations", Schema = "dbo")] 
public class Relations 
{ 

    public Relations() 
    { 
    } 
    [Key, Column(Order = 1)] 
    public Guid GenRelRefNo {get; set;} 
    public Attribute EntityColumnTo { get; set; } 
    public Attribute EntityColumnFrom { get; set; } 
    public string CollaborationCaption {get; set;} 
    public string CollaborationTooltip {get; set;} 
} 

關係模型包含2個屬性,每個屬性指向一個不同的屬性。數據庫表中的列名與它們在模型上的名稱一樣 - EntityColumnTo和EntityColumnFrom。我使用關係模式的代碼是

var res = db?.Relations 
      .Include(c => c.EntityColumnTo) 
      .Include(c => c.EntityColumnFrom) 
      .Where(x => x.EntityColumnTo.EntityRefNo == entityrefNo 
       || x.EntityColumnFrom.EntityRefNo == entityrefNo).ToList(); 

這個調用不起作用,我得到下面的錯誤,呻吟無效的列名稱。

System.Data.SqlClient.SqlException occurred 
    HResult=0x80131904 
    Message=Invalid column name 'EntityColumnFromEntityColumnRefNo'. 
Invalid column name 'EntityColumnToEntityColumnRefNo'. 
Invalid column name 'EntityColumnToEntityColumnRefNo'. 
Invalid column name 'EntityColumnFromEntityColumnRefNo'. 
Invalid column name 'EntityColumnFromEntityColumnRefNo'. 
Invalid column name 'EntityColumnToEntityColumnRefNo'. 
Invalid column name 'EntityColumnFromEntityColumnRefNo'. 
Invalid column name 'EntityColumnToEntityColumnRefNo'. 
    Source=.Net SqlClient Data Provider 
    StackTrace: 
<Cannot evaluate the exception stack trace> 

那麼,我做錯了什麼?我還有其他的例子反映了我先描述的方法,並且它們都正常工作。我確定在關係模型上有兩個聲明是相同的屬性類型是問題,需要一些額外的處理 - 框架如何知道綁定在哪裏的屬性。我已經使用擴展方法和各種其他廢話來破解代碼,使其工作,但我無法相信這是正確的做事方式。提前致謝。

回答

0

您應該將外鍵屬性添加到所有實體。 EF支持而不是這樣做,但它更困難。一旦你有明確的ForeignKey屬性,裝飾模型很簡單。 EG

[Table("Relations", Schema = "dbo")] 
public class Relations 
{ 

    public Relations() 
    { 
    } 
    [Key, Column(Order = 1)] 
    public Guid GenRelRefNo {get; set;} 

    [ForeignKey("EntityColumnToRefNo")] 
    public Attribute EntityColumnTo { get; set; } 

    [ForeignKey("EntityColumnFromRefNo")] 
    public Attribute EntityColumnFrom { get; set; } 

    public Guid EntityColumnToRefNo { get; set; } 
    public Guid EntityColumnFromRefNo { get; set; } 

    public string CollaborationCaption {get; set;} 
    public string CollaborationTooltip {get; set;} 
} 
相關問題