none
EF Migrations M2M table fluent not being scaffolded RRS feed

  • Question

  • I am using EF 4.3.1 with migrations.  I noticed that my M2M tables were not being created based on my class definitions and fluent mappings, so I added fluent mappings that specified the M2M table definition explicitly:

                modelBuilder.Entity<MailingAddress>()
                    .HasMany<AddressAssociation>(t => t.Associations)
                    .WithMany()
                    .Map(m => m
                        .MapLeftKey("MailingAddressID")
                        .MapRightKey("AddressAssociationID")
                        .ToTable("MailingAddressAssociations"));
    

    After compiling the solution, I ran:

    Add-Migration M2MTables -StartUpProjectName "DBService" -ProjectName "DBService"

    it produced a DbMigration class with empty Up() and Down() methods.  I have successfully created and processed other migrations (some of which needed to be adjusted).

    Am I doing something incorrectly or is this a gap in the EF Migrations capability?

    I am sorry if this is answered elsewhere but my 45 minutes of searching on MSDN and google did not turn up anything relevant.

    Thanks in advance,

    Kyle



    Kyle Fulton

    Wednesday, March 7, 2012 11:11 PM

Answers

  • Please re-read the first sentence of my opening question.

    I appreciate your assertion that it works, but it does not work for me nor does it give any indication why it is not working.  Turning on trace and debug logging was staggeringly unhelpful.  If I drop my database, EF recreates it properly.  But EF Migrations are not scaffolding anything but an empty class.

    EF is a really cool idea and has some wonderful architecture and ideas behind it.  It is not ready for prime time except in limited cases.  My db, with many hundreds of tables, highlights performance problems and friendliness problems.  EF was not meant to be an extensible, friendly OR mapper that plays nicely with others.  I will continue to use EF in a limited, read-only way, if I can get it to export a database with 800 tables cleanly.

    Regards and good luck.

    Kyle


    Kyle Fulton

    • Marked as answer by Kyle Fulton Friday, March 9, 2012 1:32 PM
    Friday, March 9, 2012 1:32 PM

All replies

  • Hi Kyle,

    Welcome!

    I think it should work, I'd like to share my steps:

    Here is my class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity;
    
    namespace ManytoManyEF43
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (var db= new MyContext())
                {
                    db.Database.CreateIfNotExists();
                }
            }
        }
        public class Mail
        {
            public int Id { get; set; }
            public string Title { get; set; }
          //  public string migrationtest { get; set; }//add a new property --->run add-migration
            public ICollection<Address> Addresses { get; set; }
        }
        public class Address
        {
            public int Id { get; set; }
            public string Location { get; set; }
          //  public ICollection<Mail> Mails { get; set; }
        }
        public class MyContext:DbContext
        {
            public DbSet<Mail> Mails { get; set; }
            public DbSet<Address> Addresses { get; set; }
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Mail>()
                   .HasMany(t => t.Addresses)
                   .WithMany()
                   .Map(m => m
                       .MapLeftKey("MailingAddressID")
                       .MapRightKey("AddressAssociationID")
                       .ToTable("MailingAddressAssociations"));
            }
        }
    }
    

    1. Running the code to create database firstly.

    2. Enable the Migration by "enable-migrations"

    3. Add the new column "public string migrationtest { get; set; }" 

    4. Add Migration by "add-migration test"

    5. update-database.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, March 8, 2012 8:56 AM
    Moderator
  • The example you kindly created does not replicate the situation I described. 

    The initial state of the database did not include a M2M table.  Starting with an initial state of the db without the M2M table, I added a fluent mapping that included the M2M definition, recompiled, and ran add-migration.  This add-migration looks like it successfully completed, creating a new migration class, but the migration methods were empty.

    Is there logging that I can enable on add-migration in a config file or on the parameters?

    Any reason why enabling migrations would not create an initial create migration?

    Is there documentation on the cmdlets EF Migration exposes?

    Is there documentation on using code to execute the scaffolding process EF now implements?

    As background, I enabled migrations a couple of weeks ago on an existing Code First implementation and used the scaffolding process to create 3 migrations, which included new tables, columns, and renamed tables.  I have had AutomaticMigrationsEnabled set to false.  It would not apply correctly using the update-database command, complaining about pieces not existing, but if I used a database initializer of the new MigrateDatabaseToLatestVersion type on startup, it successfully applied my modified migrations. 

    So I have been using EF migrations with a limited degree of success until this issue.  Now the scaffolding does not recognize any changes, even if I add a plain string column to one of my data classes.

    Thanks,

    Kyle


    Kyle Fulton

    Thursday, March 8, 2012 1:55 PM
  • Hi Kyle,

    Please try to use EntityFramework4.3.1.0, it works.

    =============

    uninstall-package entityframework -------NuGet command to uninstall the entityframework.dll in current project

    install-package entityframework----the version4.3.1 will be installed

    =============

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 9, 2012 7:26 AM
    Moderator
  • Please re-read the first sentence of my opening question.

    I appreciate your assertion that it works, but it does not work for me nor does it give any indication why it is not working.  Turning on trace and debug logging was staggeringly unhelpful.  If I drop my database, EF recreates it properly.  But EF Migrations are not scaffolding anything but an empty class.

    EF is a really cool idea and has some wonderful architecture and ideas behind it.  It is not ready for prime time except in limited cases.  My db, with many hundreds of tables, highlights performance problems and friendliness problems.  EF was not meant to be an extensible, friendly OR mapper that plays nicely with others.  I will continue to use EF in a limited, read-only way, if I can get it to export a database with 800 tables cleanly.

    Regards and good luck.

    Kyle


    Kyle Fulton

    • Marked as answer by Kyle Fulton Friday, March 9, 2012 1:32 PM
    Friday, March 9, 2012 1:32 PM