代码优先数据库映射,table 缺少(未创建)多对多关系
Code-first database mapping, table missing (wasn't created) for many-to-many relationship
我正在使用 Entity Framework 6.1 构建 ASP.NET webforms 应用程序,使用代码优先方法生成数据库。我有两个 tables,Product 和 Tags,处于多对多关系中。 类 如下:
public class Product
{
public long Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
public class Tag
{
public long Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products{ get; set; }
}
我想要此关系 ProductTags 和 ProductTagsTradeFor 中的两个连接点 table。所以我覆盖了 OnModelCreating 的 WebsiteDbContext.
modelBuilder.Entity<Product>().HasMany<Tag>(s => s.Tags).WithMany(c => c.Products)
.Map(cs =>
{
cs.MapLeftKey("ProductId");
cs.MapRightKey("TagId");
cs.ToTable("ProductTags");
});
modelBuilder.Entity<Product>().HasMany<Tag>(s => s.Tags).WithMany(c => c.Products)
.Map(cs =>
{
cs.MapLeftKey("ProductId");
cs.MapRightKey("TagId");
cs.ToTable("ProductTradeForTags");
});
在 运行 应用程序之后,数据库已创建并且 table ProductTradeForTags 存在,但 table ProductTags 不见了。
问题是什么?如何解决才能创建两个 table?
您无法共享导航属性。您将需要为每个添加第二组导航集合:
public class Product
{
public long Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
public virtual ICollection<Tag> TradeForTags { get; set; }
}
public class Tag
{
public long Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products{ get; set; }
public virtual ICollection<Product> TradeForProducts{ get; set; }
}
然后
modelBuilder.Entity<Product>().HasMany(s => s.Tags).WithMany(c => c.Products)
.Map(cs =>
{
cs.MapLeftKey("ProductId");
cs.MapRightKey("TagId");
cs.ToTable("ProductTags");
});
modelBuilder.Entity<Product>().HasMany(s => s.TradeForTags).WithMany(c => c.TradeForProducts)
.Map(cs =>
{
cs.MapLeftKey("ProductId");
cs.MapRightKey("TagId");
cs.ToTable("ProductTradeForTags");
});
您的模型需要从标签导航到产品以及从产品导航到标签。
在这种情况下,一个关联 table 就足够了。
EF 应该引发异常,但它只是忽略了第一个配置。
我正在使用 Entity Framework 6.1 构建 ASP.NET webforms 应用程序,使用代码优先方法生成数据库。我有两个 tables,Product 和 Tags,处于多对多关系中。 类 如下:
public class Product
{
public long Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
public class Tag
{
public long Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products{ get; set; }
}
我想要此关系 ProductTags 和 ProductTagsTradeFor 中的两个连接点 table。所以我覆盖了 OnModelCreating 的 WebsiteDbContext.
modelBuilder.Entity<Product>().HasMany<Tag>(s => s.Tags).WithMany(c => c.Products)
.Map(cs =>
{
cs.MapLeftKey("ProductId");
cs.MapRightKey("TagId");
cs.ToTable("ProductTags");
});
modelBuilder.Entity<Product>().HasMany<Tag>(s => s.Tags).WithMany(c => c.Products)
.Map(cs =>
{
cs.MapLeftKey("ProductId");
cs.MapRightKey("TagId");
cs.ToTable("ProductTradeForTags");
});
在 运行 应用程序之后,数据库已创建并且 table ProductTradeForTags 存在,但 table ProductTags 不见了。
问题是什么?如何解决才能创建两个 table?
您无法共享导航属性。您将需要为每个添加第二组导航集合:
public class Product
{
public long Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
public virtual ICollection<Tag> TradeForTags { get; set; }
}
public class Tag
{
public long Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products{ get; set; }
public virtual ICollection<Product> TradeForProducts{ get; set; }
}
然后
modelBuilder.Entity<Product>().HasMany(s => s.Tags).WithMany(c => c.Products)
.Map(cs =>
{
cs.MapLeftKey("ProductId");
cs.MapRightKey("TagId");
cs.ToTable("ProductTags");
});
modelBuilder.Entity<Product>().HasMany(s => s.TradeForTags).WithMany(c => c.TradeForProducts)
.Map(cs =>
{
cs.MapLeftKey("ProductId");
cs.MapRightKey("TagId");
cs.ToTable("ProductTradeForTags");
});
您的模型需要从标签导航到产品以及从产品导航到标签。
在这种情况下,一个关联 table 就足够了。
EF 应该引发异常,但它只是忽略了第一个配置。