CTP5 vs release 4.1 EF from many to many relationships

This code does not work. What's wrong? OnModelCreating has no effect? Because I do not see the table "ProductCategories" 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.

+4
source share
1 answer

This is what I tried in the console application and it works as expected:

 namespace Q7122388 { #region Imports using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Linq; #endregion 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; } } public class DatabaseContext : DbContext { public virtual DbSet<Product> Products { get; set; } public virtual DbSet<Category> Categories { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Product>() .HasMany<Category>(m => m.Categories) .WithMany().Map(m => m.MapLeftKey("ProductId") .MapRightKey("CategoryId") .ToTable("ProductCategories")); base.OnModelCreating(modelBuilder); } } class Program { static void Main(string[] args) { Database.SetInitializer(new DropCreateDatabaseAlways<DatabaseContext>()); using (var context = new DatabaseContext()) context.Database.Initialize(true); } } } 
+1
source

All Articles