EF如何具有多对一关系?

时间:2020-05-05 15:26:16

标签: c# entity-framework nhibernate entity-framework-core

上下文:

我有关系

class Service {
   public virtual int Sequencial { get; set; }
   public virtual string Name { get; set; }
   public virtual ServiceType Type { get; set; }
   ...
}

class ServiceType {
   public virtual int Sequencial { get; set; }
   public virtual string Name { get; set; }
}


在NHibernate类中看起来像这样

Id(s => s.Sequencial, “ SERVICE_ID”)。GeneratedBy.Sequence(“ SQ_SERVICE_ID”);
引用(s => s.ServiceType,“ SERVICE_TYPE_ID”);
地图(a => a.Nome,“ SERVICE_NAME”);
...

我如何实现 References(s => s.ServiceType,“ SERVICE_TYPE_ID”),换句话说,如何在EF中实现多对一?

1 个答案:

答案 0 :(得分:0)

class Service {
   public virtual int Sequencial { get; set; }
   public virtual string Name { get; set; }
   public virtual ServiceType Type { get; set; }
 }

class ServiceType {
  public virtual int Sequencial { get; set; }
  public virtual string Name { get; set; }
  public IList<Service> Services{get;set;}
 }

使用这两个链接获得更多指导

asp

https://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx

asp核心

https://www.entityframeworktutorial.net/efcore/one-to-many-conventions-entity-framework-core.aspx