CTP5 与 4.1 版 EF 多对多关系
这段代码不起作用。怎么了? OnModelCreating 不会影响任何结果?因为我在数据库中看不到“ProductCategories”表。
public class GoldContext : DbContext
{
public virtual DbSet<Prouct> Products { get; set; }
public virtual DbSet<Category> Categories { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//HACK:4.1 modelBuilder.Conventions.Add(new DecimalPrecisionAttributeConvention());
modelBuilder.Entity<Product>()
.HasMany<Category>(m => m.Categories)
.WithMany().Map(m =>
m.MapLeftKey("ProductId")
.MapRightKey("CategoryId")
.ToTable("ProductCategories"));
base.OnModelCreating(modelBuilder);
}
}
//product and category classes look like this.
public class Product
{
[Key]
public int Id { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<Category> Categories { get; set; }
}
public class Category
{
[Key]
public int Id { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
提前致谢。
This code doesn't work. What is wrong? OnModelCreating doesnt't effect any result? Because I can not see "ProductCategories" table in my Database.
public class GoldContext : DbContext
{
public virtual DbSet<Prouct> Products { get; set; }
public virtual DbSet<Category> Categories { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//HACK:4.1 modelBuilder.Conventions.Add(new DecimalPrecisionAttributeConvention());
modelBuilder.Entity<Product>()
.HasMany<Category>(m => m.Categories)
.WithMany().Map(m =>
m.MapLeftKey("ProductId")
.MapRightKey("CategoryId")
.ToTable("ProductCategories"));
base.OnModelCreating(modelBuilder);
}
}
//product and category classes look like this.
public class Product
{
[Key]
public int Id { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<Category> Categories { get; set; }
}
public class Category
{
[Key]
public int Id { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我在控制台应用程序中尝试过的并且按预期工作:
This is what I've tried in a console application and works as expected :