1

vir我正在尝试使用实体框架代码优先在我的 ASP.NET MVC4 应用程序中创建一个查找表。它适用于我们的位置,应该有两个条目。我需要某种与它们相关联的 ID,因此我的 Software 表中存储了一个 LocationID。但是,当我创建它们时,会为软件表中的每一行创建一个条目。

这是我的软件课:

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 class Location
    {
        public int Id { get; set; }
        [Required]
        [StringLength(20)]
        public string LocationName { get; set; }
        public virtual Software Software { get; set; }
    }

这是我对种子方法的 Global.asax 调用:

Database.SetInitializer(new SampleData());
            using (var context = new Context())
            {
               context.Database.Initialize(true);
            }

这是我的背景:

 public class Context : DbContext
    {
        public DbSet<Software> Software { get; set; }
        public DbSet<Location> Locations { get; set; }
        public DbSet<SoftwarePublisher> SoftwarePublishers { get; set; }
        public DbSet<SoftwareType> SoftwareTypes { get; set; }

        public Context()
        {
           Configuration.ProxyCreationEnabled = false;
        }
    }

这是我的播种:

public class SampleData : CreateDatabaseIfNotExists<Context>
    {
        protected override void Seed(Context context)
        {
            new List<Software> {
                    new Software { 
                         Title = "Adobe Creative Suite", 
                         Version = "CS6", 
                         SerialNumber = "1234634543", 
                         Platform = "Mac", 
                         Notes = "Macs rock!", 
                         PurchaseDate = "2012-12-04", 
                         Suite = true, 
                         SubscriptionEndDate = null, 
                         SeatCount = 0, 
                         SoftwareTypes = new List<SoftwareType> 
                               { new SoftwareType { Type="Suite" }}, 
                         Locations = new List<Location> 
                               { new Location { LocationName = "Paradise" }},
                         Publishers = new List<SoftwarePublisher> 
                               { new SoftwarePublisher { Publisher = "Adobe" }}},
                ...other similar rows...
                }.ForEach(s => context.Software.Add(s));

            base.Seed(context);
            context.SaveChanges(); 

        }

因为我正在为 Locations 之类的东西创建一个新列表(我需要修复 SoftwareType 和 Publisher 等其他东西,但让我们专注于 Locations),它正在我的 Locations 表中创建一个新行。如何重组我的类,以便我的 Locations 表中有两个条目,然后我的 Software 表中的 ID 指向这两个条目之一?请记住我是一个实体框架新手,所以请尽量明确。谢谢。

4

1 回答 1

0

我认为您想要软件和位置之间的多对多关系。为此,您需要创建一个连接表(也称为链接表)。我相信您想在 OnModelCreating 覆盖中执行此操作

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

我从这篇博文中得到了那个片段

于 2012-12-06T16:57:37.867 回答