Search code examples
entity-frameworkentity-framework-4entity-framework-4.1ef-code-firstentity-framework-ctp5

CTP5 vs release 4.1 EF many to many relationship


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.


Solution

  • This is what I've tried in a console application and 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);
            }
        }
    }