在利用Code Frist模式时,容易碰到模型结构改变后,怎么样使数据库中对应的表结构自动进行更新。可以设定为自动进行更新模式。具体代码如下:
internal sealed class Configuration : DbMigrationsConfiguration<LmAuthenticatCenterContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}
protected override void Seed(LmAuthenticatCenterContext context)
{
base.Seed(context);
}
}
public partial class LmAuthenticatCenterContext : DbContext
{
static LmAuthenticatCenterContext()
{
//Database.SetInitializer<LmAuthenticatCenterContext>(new MigrateDatabaseToLatestVersion<LmAuthenticatCenterContext, Configuration>());
Database.SetInitializer<LmAuthenticatCenterContext>(null);
}
public LmAuthenticatCenterContext()
: base("Name=LmAuthenticatCenterContext")
{
}
public DbSet<Authen_User> Authen_User { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new Authen_UserMap());
}
}