实体框架相当于多个连接?

我实际上不确定这是继承的情况还是完全其他的情况。我的数据库中有一组 3 个表。GeoSales、CountryRef 和 RegionRef。EF 设计器GeoSales 表有一个 CountryID,它是 CountryRef 表的外键。Countryref 表有一个 regionID ,它是 Regionref 表的外键。每个 GeoSale 都有一个 CountryID,每个 Countryref 都有一个 RegionID。还有一个 Location 表,它有一个外键 locationID 到 Geosales 表


在这种情况下,我希望可以在我的应用程序中显示 RegionName 字段。我主要是一名 SQL 开发人员,所以我在 C# 方面不是很擅长,而且我看到的绝大多数教程都是针对代码优先方法的,并且有一堆我不太了解的 C# 代码。在 Sql 中,我只会使用 2 个这样的连接:


SELECT * FROM GeoSales G 

JOIN CountryRef C ON C.CountryId = G.CountryID 

JOIN RegionRef R ON R.RegionID = C.RegionID

JOIN Location L ON L.LocationdiD = G.LocationID

在我的 MVC/EF 项目中,我将如何以视图可以将区域信息包含在 GeoSales 记录中的方式创建这种关系?


我考虑过的一件事是,除了 countryref 表之外,还向 GeoSales 表的 regionref 表中添加了 FK,但我不确定这是否会导致我正在寻找的行为。Geosale 记录上可以有 1 个地区和 1 个国家/地区,一个地区可以有多个国家/地区,一个国家/地区可以有多个 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; }

    }


慕无忌1623718
浏览 104回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP