1

I have a very simple context

using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace WindowsFormsApplication1
{
    [Table("MyParts2")]
    public class MyPart2
    {
        [Browsable(false)]
        [Key]
        public int Id { get; set; }
    }


    public class EFProjectDbContext : DbContext
    {
        public EFProjectDbContext() // used for migrations
            : base("name=ApplicationDatabase")
        {
            Database.SetInitializer(new CreateDatabaseIfNotExists<EFProjectDbContext>());
        }

        public EFProjectDbContext(string connectionString)
            : base(connectionString)
        {
            if (Database.Exists())
            {
                // error here is benign uncheck break 
                var isCompatible = Database.CompatibleWithModel(false); //error occurs here
                if (!isCompatible)
                {
                    // uncheck break when this exception type is shown
                    // note if you are in debug you need to run exe to kick off upgrade
                    throw new Exception(
                        "The database is not compatible with the entity model. Drop the database or run migrations");
                }
            }
            else
            {
                Database.SetInitializer(new CreateDatabaseIfNotExists<EFProjectDbContext>());
            }
        }

        public EFProjectDbContext(DbConnection connection)
            : base(connection, false)
        {
        }

        public DbSet<MyPart2> MyParts2 { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
        }
    }
}

And i have migrations enabled

If I rename the class name and table attribute and create a migration the up method creates as

 public override void Up()
    {
        RenameTable(name: "dbo.MyParts2", newName: "MyParts3");
    }

How does it the create migration realise that the table is a rename and not a drop and re-create ?

Kirsten
  • 15,730
  • 41
  • 179
  • 318
  • I suspect that sometimes it does not , and the rename needs to be hand coded. As it has been http://stackoverflow.com/questions/13296996/entity-framework-migrations-renaming-tables-and-columns. In this scenario I have often needed to cause a further migration... by some dummy change so that the migration sequence will run. – Kirsten Mar 07 '16 at 21:13

0 Answers0