简体   繁体   English

EF Core:Map 没有导航属性的多对多关系

[英]EF Core: Map Many to Many relationship without navigation property

I'm trying to create a many to many relationship in entity Framework Core using Fluent API Here is my first model:我正在尝试使用 Fluent API 在实体框架核心中创建多对多关系 这是我的第一个 model:

MyCalculationSelector.cs

  public int SortOrder { get; set; }
  public string UserString { get { return Name; } }

  private IList<CalculationType> _calculationTypes;
  public virtual IList<CalculationType> CalculationTypes
    {
       get { return _calculationTypes; }
       set { _calculationTypes = value; }
    }

And this is my second model:这是我的第二个 model:

MyCalculationType.cs

 public int SortOrder { get; set; }

 public string UserString
 {
    get { return Name; }
 }

 public int CalculationMethod { get; set; }

I remember that EF 6 could easily make the many to many relationship from Fluent API:我记得 EF 6 可以轻松地从 Fluent API 建立多对多关系:

modelBuilder.Entity<MyCalculationSelector>().HasMany(x => x.MyCalculationTypes).WithMany();

Can we something like this be achieved in ef core?我们可以在 ef core 中实现这样的事情吗? as of today, Hasmany-Withmany implementation is not possible截至今天,Hasmany-Withmany 实现是不可能的

You can represent this with a private field or property:您可以使用私有字段或属性来表示:

modelBuilder.Entity<MyCalculationSelector>()
    .HasMany(x => x.MyCalculationTypes)
    .WithMany("NameOfThePrivateFieldOnMyCalculationType");

This way the navigation property is not exposed publically, but you can still use the Navigation Property on MyCalculationSelector without needing to traverse the intermediate join table.这样导航属性不会公开,但您仍然可以在MyCalculationSelector上使用导航属性,而无需遍历中间连接表。

I have followed the documentation and have come up with this answer, please, someone who knows more than me and will correct me if I am wrong.我已遵循文档并提出了这个答案,拜托,比我了解更多的人,如果我错了,会纠正我。

I first created my join table:我首先创建了我的连接表:

CalculationSelectorCalculationType.cs

public class CalculationSelectorCalculationType
    {
        public int CalculationSelector_Id { get; set; }
        public CalculationSelector CalculationSelector { get; set; }

        public int CalculationType_Id { get; set; }
        public CalculationType CalculationType { get; set; }
    }

CalculationSelector.cs

public CalculationSelector()
   {
      _calculationTypes = new List<CalculationSelectorCalculationType>();
      FontSize = 30;
   }
public int SortOrder { get; set; }
public string UserString { get { return Name; } }

private IList<CalculationSelectorCalculationType> _calculationTypes;
public virtual IList<CalculationSelectorCalculationType> CalculationTypes
    {
       get { return _calculationTypes; }
       set { _calculationTypes = value; }
    }

CalculationType.cs

 public int SortOrder { get; set; }

 public string UserString
 {
    get { return Name; }
 }

 public int CalculationMethod { get; set; }

...and finally my DbContext: ...最后是我的 DbContext:

modelBuilder.Entity<CalculationSelectorCalculationType>().HasKey(p => new { p.CalculationSelector_Id, p.CalculationType_Id });
modelBuilder.Entity<CalculationSelectorCalculationType>().HasOne(p => p.CalculationSelector).WithMany(x => x.CalculationTypes).HasForeignKey(p => p.CalculationSelector_Id);
modelBuilder.Entity<CalculationSelectorCalculationType>().HasOne(p => p.CalculationType).WithMany().HasForeignKey(p => p.CalculationType_Id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF Core:域中没有导航属性的一对多关系 - EF Core: One to many relationship without navigation property in domain 在EF Core多对多关系中创建直接导航属性 - Create Direct Navigation Property in EF Core Many to Many Relationship EF Core 排除查询中的某些属性,多对多关系 - EF Core exclude certain property in query, many-to-many relationship 如何使用 EF Core 5 仅在一侧(在集合端)配置与导航属性的一对多关系? - How to configure one-to-many relationship with navigation property only on one side (on collection side) with EF Core 5? 在实体框架核心的多对多关系中获取导航属性时出错 - Error in getting navigation property in a many to many relationship in entity framework core EF Core - 无法确定导航表示的关系(一对多) - EF Core - Unable to determine the relationship represented by navigation (One-to-Many) EF 6 Core 前缀基于多对一关系中实体的属性 - EF 6 Core prefix based on property of the entity in many-to-one relationship EF Core - 课堂上的多对多关系 - EF Core - Many to many relationship on a class 验证多对多关系 ef core - Validation many to many relationship ef core 使用多对多关系保存数据 EF 核心 - Save data with many to many relationship EF core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM