简体   繁体   English

如何在MVC4和实体代码中首先在Fluent中编写多对多关系

[英]How to write a many-to-many relationship in Fluent in MVC4 and Entity Code First

I'm trying to write a many-to-many relationship in the override of the onModelCreating method of my context in ASP.NET MVC4. 我正在尝试在ASP.NET MVC4中上下文的onModelCreating方法的重写中编写多对多关系。 I think I have my classes wrong because I'm getting errors in Intellisense that I don't understand. 我认为我的班级错了,因为我收到了我不理解的Intellisense错误。 Here is my override: 这是我的替代:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     base.OnModelCreating(modelBuilder);

      modelBuilder.Entity<Software>().
          HasMany(i => i.LocationId).
          WithMany(c => c.SoftwareId).
          Map(mc =>
          {
               mc.MapLeftKey("SoftwareId");
               mc.MapRightKey("LocationId");
               mc.ToTable("SoftwareLocations");
          });

 }

Here is my Software class: 这是我的软件课程:

public class Software
    {
        public int Id { get; set; }
        public virtual List<SoftwareType> SoftwareTypes { get; set; }
        public virtual List<Location> Locations { get; set; }
        public virtual List<SoftwarePublisher> Publishers { get; set; }
        [Required]
        [StringLength(128)]
        public string Title { get; set; }
        [Required]
        [StringLength(10)]
        public string Version { get; set; }
        [Required]
        [StringLength(128)]
        public string SerialNumber { get; set; }
        [Required]
        [StringLength(3)]
        public string Platform { get; set; }
        [StringLength(1000)]
        public string Notes { get; set; }
        [Required]
        [StringLength(15)]
        public string PurchaseDate { get; set; }
        public bool Suite { get; set; }
        public string SubscriptionEndDate { get; set; }
        //[Required]
        //[StringLength(3)]
        public int SeatCount { get; set; }
        public virtual Location LocationId { get; set; }
    }

Here is my Location class: 这是我的位置课程:

public class Location
    {
        public int Id { get; set; }
        [Required]
        [StringLength(20)]
        public string LocationName { get; set; }
        public virtual Software SoftwareId { get; set; }
    }

How do I write my Fluent override so I can map them correctly? 如何编写Fluent替代,以便可以正确映射它们?

Many-to-Many means that in both entities is Collections. 多对多意味着在两个实体中都是集合。 So you should set HasMany(software=>software.Locations) and set WithMany(location=>location.Softwares) as in example: 因此,您应该设置HasMany(software => software.Locations)并设置WithMany(location => location.Softwares),例如:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     base.OnModelCreating(modelBuilder);

      modelBuilder.Entity<Software>().
          HasMany(i => i.Locations).
          WithMany(c => c.Softwares).
          Map(mc =>
          {
               mc.MapLeftKey("SoftwareId");
               mc.MapRightKey("LocationId");
               mc.ToTable("SoftwareLocations");
          });

 }

Here is my Software class: 这是我的软件课程:

public class Software
{
    public int Id { get; set; }
    public virtual List<SoftwareType> SoftwareTypes { get; set; }
    public virtual ICollection <Location> Locations { get; set; }
    public virtual List<SoftwarePublisher> Publishers { get; set; }
    [Required]
    [StringLength(128)]
    public string Title { get; set; }
    [Required]
    [StringLength(10)]
    public string Version { get; set; }
    [Required]
    [StringLength(128)]
    public string SerialNumber { get; set; }
    [Required]
    [StringLength(3)]
    public string Platform { get; set; }
    [StringLength(1000)]
    public string Notes { get; set; }
    [Required]
    [StringLength(15)]
    public string PurchaseDate { get; set; }
    public bool Suite { get; set; }
    public string SubscriptionEndDate { get; set; }
    //[Required]
    //[StringLength(3)]
    public int SeatCount { get; set; }
}

Here is my Location class: 这是我的位置课程:

 public class Location
        {
            public int Id { get; set; }
            [Required]
            [StringLength(20)]
            public string LocationName { get; set; }
            public virtual ICollection<Software> Softwares { get; set; }
        }

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

相关问题 如何在实体代码优先中以多对多关系为数据库播种 - How to seed the database with a many-to-many relationship in Entity Code First Entity Framework 4.1 - Code First:多对多关系 - Entity Framework 4.1 - Code First: many-to-many relationship 代码首先添加具有多对多关系的实体 - Code first add entity with many-to-many relationship MVC-如何在DB和EDMX中最佳实现与TPT派生实体(DB优先)的多对多关系 - MVC - How to best implement Many-To-Many relationship to TPT Derived Entity (DB First) in DB and EDMX 如何首先使用Entity Framework代码创建多对多关系? - How do I create a many-to-many relationship with Entity Framework code first? 实体框架,代码优先,如何从多对多关系映射中向表添加主键? - Entity Framework, code-first, how to add primary key to table out of many-to-many relationship mapping? 流畅的nhibernate - 在同一实体上的多对多关系映射 - fluent nhibernate - many-to-many relationship mapping on same entity 首先创建多对多关系代码 - Creating many-to-many relationship code first 没有流利的API的C#多对多关系代码 - c# many-to-many relationship code first without fluent API 如何通过Fluent API实体框架定义多对多关系? - How to define Many-to-Many relationship through Fluent API Entity Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM