如何在使用 EF 数据迁移时将旧数据保留在数据库中
我认为 EF DataMigrations 的目的是在更改数据库结构的同时保留旧数据。我想的对吗?但是当我通过迁移更新数据库时,所有旧数据都会被删除。
我是否做错了什么或者在这种情况下不可能使用数据迁移?
这是 DbContext:
public class CodeFirstContext : DbContext
{
public CodeFirstContext() : base ("ZenRandevuIzle")
{
}
public DbSet<User> Users { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<Takvim> Takvims { get; set; }
public DbSet<Musteri> Musteris { get; set; }
public DbSet<Randevu> Randevus { get; set; }
public DbSet<SeansTuru> SeansTurus { get; set; }
public DbSet<Hizmet> Hizmets { get; set; }
public DbSet<Islem> Islems { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
}
}
这是 Global.asax:
Database.SetInitializer<CodeFirstContext>(new CodeFirstContextInit());
这是迁移配置:
internal sealed class Configuration : DbMigrationsConfiguration<CodeFirstContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
}
I was thinking that the purpose of EF DataMigrations is to preserve old data while changing the database structure. Do I think right? But when I update the database with migrations all the old data is erased.
Do I make something wrong or maybe it isn't possible to use data migrations in this kind of scenario ?
this is DbContext:
public class CodeFirstContext : DbContext
{
public CodeFirstContext() : base ("ZenRandevuIzle")
{
}
public DbSet<User> Users { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<Takvim> Takvims { get; set; }
public DbSet<Musteri> Musteris { get; set; }
public DbSet<Randevu> Randevus { get; set; }
public DbSet<SeansTuru> SeansTurus { get; set; }
public DbSet<Hizmet> Hizmets { get; set; }
public DbSet<Islem> Islems { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
}
}
this is Global.asax:
Database.SetInitializer<CodeFirstContext>(new CodeFirstContextInit());
this is migration Config:
internal sealed class Configuration : DbMigrationsConfiguration<CodeFirstContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CodeFirstContextInit 初始化程序是否会删除数据库并重新创建它(它的基类是什么?)通常在使用迁移时我不会有初始化程序。
Does the CodeFirstContextInit initialiser drop the database and recreate it (what is its base class?) normally I wouldn't have an initializer when using migrations.
要将 EF 4.3 Code First 数据迁移与现有数据库结合使用,您需要创建一个“空”迁移,以便创建迁移历史记录表并建立基线。
本文中概述的步骤应该会有所帮助 http://thedatafarm.com/data-access/using-ef-4-3-code-first-migrations-with-an-existing-database
To use EF 4.3 Code First Data Migrations with an existing database you need to create an "empty" migration so that the Migration-History table is created and a baseline can be established.
The Steps outlined in this post should help http://thedatafarm.com/data-access/using-ef-4-3-code-first-migrations-with-an-existing-database