我使用Entity Framework创建了一个项目,该项目使用Migrations更改数据库并创建其初始数据。
好的,但是当我更改某些类时,它们会自动更改数据库实体。我不想这种行为,我想更改模型,并为该更改创建一个迁移。
该怎么做?
[DbConfigurationType(typeof(MySql.Data.EntityFramework.MySqlEFConfiguration))]
public class AcmeDBContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new AssociadoEntityConfiguration());
modelBuilder.Configurations.Add(new DepartamentoEntityConfiguration());
modelBuilder.Configurations.Add(new CategoriaEntityConfiguration());
modelBuilder.Configurations.Add(new ProdutoEntityConfiguration());
modelBuilder.Configurations.Add(new UsuarioEntityConfiguration());
modelBuilder.Configurations.Add(new PermissaoEntityConfiguration());
base.OnModelCreating(modelBuilder);
}
public AcmeDBContext()
: base("CooperyaraDB")
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<AcmeDBContext, LogDataMigration>(true));
}
//db sets was where
}
public class FixedMySqlMigrationSqlGenerator : MySqlMigrationSqlGenerator
{
public FixedMySqlMigrationSqlGenerator()
: base()
{
}
protected override MigrationStatement Generate(CreateIndexOperation op)
{
if (!op.IsUnique)
op.AnonymousArguments.Add("Type", "BTrees");
return base.Generate(op);
}
}
public sealed class LogDataMigration : DbMigrationsConfiguration<AcmeDBContext>
{
private static readonly HashSet<string> _InitializedConnections = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
private static readonly object _LockProxy = new object();
public LogDataMigration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = false;
SetSqlGenerator("MySql.Data.MySqlClient", new FixedMySqlMigrationSqlGenerator());
}
protected override void Seed(AcmeDBContext context)
{
base.Seed(context);
}
}