簡體   English   中英

使用Entity Framework Core中的Include檢索字段名稱與主鍵不匹配的相關數據

[英]Using Include in Entity Framework Core to retrieve related data where the field name doesn't match the primary key

我是EF的新手(首先使用Core),並且一直在努力解決問題。

簡而言之,我有2個彼此一對一關聯的實體。 在一個可以正常工作的示例中,我有一個與“子”實體相關的“父”實體,如下所示

[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的Attribute表上的物理列名稱正好是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>

那么,我在做什么錯呢? 我還有其他示例,它們都與我首先描述的方法相仿,並且它們都能正常工作。 我確定在問題模型上對於相同的Attributes類型有2個聲明是問題所在,並且需要一些額外的處理-框架如何知道將什么屬性綁定到哪里。 我已經破解了代碼,使其能夠使用擴展方法和各種其他廢話來工作,但是我無法相信這是正確的處理方式。 提前致謝。

您應該將外鍵屬性添加到所有實體。 EF支持這樣做,但要困難得多。 一旦有了顯式的ForeignKey屬性,就可以輕松裝飾模型。 例如

[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;}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM