0

ASP.NETMVC4のコンテキストの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 に答える