EF 自动修改数据库

Posted 一步一个脚印

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EF 自动修改数据库相关的知识,希望对你有一定的参考价值。

1.配置类

internal sealed class Configuration<TContext> : DbMigrationsConfiguration<TContext> where TContext : DbContext
{
public Configuration()
{
    AutomaticMigrationsEnabled = true;

    AutomaticMigrationDataLossAllowed = true;
}

protected override void Seed(TContext context)
{

}
}

 2.初始化类

public class SchoolContext : DbContext
{
public SchoolContext() : base("name=test")
{
}
static SchoolContext()
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<SchoolContext, Configuration<SchoolContext>>());
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   modelBuilder.Configurations.AddFromAssembly(Assembly.GetExecutingAssembly());

}
}

3.实体类

public class Course { public int StudentID { get; set; } public string Name { get; set; } }

好。搞定。就这么简单。我也研究了半天,看了很多资料。就可以修改实体类看看数据库的变法吧。

以上是关于EF 自动修改数据库的主要内容,如果未能解决你的问题,请参考以下文章