我有一个非常简单的背景
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>();
}
}
}
我启用了迁移
如果我重命名类名和表属性并创建迁移,则up方法将创建为
public override void Up()
{
RenameTable(name: "dbo.MyParts2", newName: "MyParts3");
}
创建迁移如何实现该表是重命名而不是删除并重新创建?