我實際上不確定這是繼承的情況還是完全其他的情況。我的數(shù)據(jù)庫中有一組 3 個表。GeoSales、CountryRef 和 RegionRef。EF 設(shè)計器GeoSales 表有一個 CountryID,它是 CountryRef 表的外鍵。Countryref 表有一個 regionID ,它是 Regionref 表的外鍵。每個 GeoSale 都有一個 CountryID,每個 Countryref 都有一個 RegionID。還有一個 Location 表,它有一個外鍵 locationID 到 Geosales 表在這種情況下,我希望可以在我的應(yīng)用程序中顯示 RegionName 字段。我主要是一名 SQL 開發(fā)人員,所以我在 C# 方面不是很擅長,而且我看到的絕大多數(shù)教程都是針對代碼優(yōu)先方法的,并且有一堆我不太了解的 C# 代碼。在 Sql 中,我只會使用 2 個這樣的連接:SELECT * FROM GeoSales G JOIN CountryRef C ON C.CountryId = G.CountryID JOIN RegionRef R ON R.RegionID = C.RegionIDJOIN Location L ON L.LocationdiD = G.LocationID在我的 MVC/EF 項目中,我將如何以視圖可以將區(qū)域信息包含在 GeoSales 記錄中的方式創(chuàng)建這種關(guān)系?我考慮過的一件事是,除了 countryref 表之外,還向 GeoSales 表的 regionref 表中添加了 FK,但我不確定這是否會導(dǎo)致我正在尋找的行為。Geosale 記錄上可以有 1 個地區(qū)和 1 個國家/地區(qū),一個地區(qū)可以有多個國家/地區(qū),一個國家/地區(qū)可以有多個 geosales。這是我的 GeoSales 課程public partial class GeoSale{ public int ID { get; set; } public int LocationID { get; set; } public Nullable<int> CountryId { get; set; } public Nullable<decimal> Amount { get; set; } public Nullable<int> Year { get; set; } public virtual CountryRef CountryRef { get; set; } public virtual Location Location { get; set; }}}這是我的 CountryRef 課程 public partial class CountryRef { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public CountryRef() { this.GeoSales = new HashSet<GeoSale>(); } public int CountryID { get; set; } public string CountryName { get; set; } public Nullable<int> RegionID { get; set; } public virtual RegionRef RegionRef { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<GeoSale> GeoSales { get; set; } }
實體框架相當(dāng)于多個連接?
慕無忌1623718
2021-06-04 06:00:16