簡體   English   中英

如何創建沒有外鍵和列的導航屬性的名稱不同

[英]How to create Navigation Properties without foreign keys and columns are named differently

例如,我有2個表:

public class Entity1
{
    public decimal SodaId { get; set; }
    public virtual Entity2 Entity2 { get; set; } 
}

public class Entity2
{
    public decimal Id { get; set; }
    public string PopId { get; set; }
    public virtual ICollection<Entity1> Entity1 { get; set; }
}

1&2有一對多的關系。 PopId是Entity1的外鍵。 在數據庫中沒有建立FK關系。 並且由於列名不同,EF不會自動看到該關系。

我想為這兩個表設置導航屬性,但是在弄清楚該怎么做時遇到麻煩? 我想先使用Fluent API和代碼。 任何幫助是極大的贊賞。

在您的表格中,您說實體2可以有多個實體1

public class Entity1
    {
        public decimal SodaId { get; set; }
        [ForeignKey("Entity2")]
        public int Id { get; set; }
        public virtual Entity2 Entity2 { get; set; }
    }


public class Entity2
    {
        public int Id { get; set; }
        public string PopId { get; set; }
        public virtual ICollection<Entity1> Entity1 { get; set; }
    }

這應該工作正常...

暫無
暫無

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

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