0

我正在尝试在 ASP.NET MVC4 中覆盖我的上下文的 onModelCreating 方法中编写多对多关系。我认为我的课程错了,因为我在 Intellisense 中遇到了我不理解的错误。这是我的覆盖:

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");
          });

 }

这是我的软件课:

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; }
    }

这是我的位置类:

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

如何编写 Fluent 覆盖以便正确映射它们?

4

1 回答 1

0

多对多意味着在两个实体中都是集合。所以你应该设置 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");
          });

 }

这是我的软件课:

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; }
}

这是我的位置类:

 public class Location
        {
            public int Id { get; set; }
            [Required]
            [StringLength(20)]
            public string LocationName { get; set; }
            public virtual ICollection<Software> Softwares { get; set; }
        }
于 2012-12-06T19:07:15.077 回答