I've created an ASP.NET MVC sample application in Visual Studio. I added two classes like below :
我在Visual Studio中创建了一个ASP.NET MVC示例应用程序。我添加了两个类,如下所示:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public int Price { get; set; }
public ICollection<Product> Products { get; set; }
}
public class Order
{
public int Id{ get; set; }
public Product Product { get; set; }
public int ProductId { get; set; }
public DateTime RegisterDate { get; set; }
}
and then I added two DbSet
to the main DbContext
like this:
然后我将两个DbSet添加到主DbContext中,如下所示:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public IDbSet<Product> Products { get; set; }
public IDbSet<Order> Orders { get; set; }
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
But now when I run project, I get this error :
但是现在当我运行项目时,我收到此错误:
The model backing the 'ApplicationDbContext' context has changed since the database was created
自创建数据库以来,支持'ApplicationDbContext'上下文的模型已更改
I just wanted to know is there a way to update the database without using migrations (Add-migrations
and update-database
)?
我只是想知道有没有办法在不使用迁移(Add-migrations和update-database)的情况下更新数据库?
2 个解决方案
#1
3
You can update the database structure in the way you want (queries, via DBMS user interface and so on).
If you do so you need to disable configuration check.
您可以按照自己的方式更新数据库结构(查询,通过DBMS用户界面等)。如果这样做,则需要禁用配置检查。
Database.SetInitializer<YourContext>(null);
(I insert this in a static constructor in the context)
(我在上下文中的静态构造函数中插入此内容)
#2
2
You can enable automatic migrations to achieve this. This approach definitely uses code-first migration but you don't need to use Add-Migration command or update-database.
您可以启用自动迁移来实现此目的。这种方法肯定使用代码优先迁移,但您不需要使用Add-Migration命令或update-database。
Run Enable-Migrations –EnableAutomaticMigrations from package manager console first to generates a Configuration class as follows:
首先从程序包管理器控制台运行Enable-Migrations -EnableAutomaticMigrations以生成Configuration类,如下所示:
internal sealed class Configuration : DbMigrationsConfiguration<ApplicationDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(ApplicationDbContext context)
{
}
}
Now you could either update the db context's constructor to migrate to latest version as follows:
现在,您可以更新db上下文的构造函数以迁移到最新版本,如下所示:
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
Database.SetInitializer(
new MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>());
this.Database.Initialize(true);
}
Or migrate to the latest version on application start up as follows:
或者在应用程序启动时迁移到最新版本,如下所示:
using (var context = new ApplicationDbContext())
{
Database.SetInitializer(
new MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>());
context.Database.Initialize(true);
}
#1
3
You can update the database structure in the way you want (queries, via DBMS user interface and so on).
If you do so you need to disable configuration check.
您可以按照自己的方式更新数据库结构(查询,通过DBMS用户界面等)。如果这样做,则需要禁用配置检查。
Database.SetInitializer<YourContext>(null);
(I insert this in a static constructor in the context)
(我在上下文中的静态构造函数中插入此内容)
#2
2
You can enable automatic migrations to achieve this. This approach definitely uses code-first migration but you don't need to use Add-Migration command or update-database.
您可以启用自动迁移来实现此目的。这种方法肯定使用代码优先迁移,但您不需要使用Add-Migration命令或update-database。
Run Enable-Migrations –EnableAutomaticMigrations from package manager console first to generates a Configuration class as follows:
首先从程序包管理器控制台运行Enable-Migrations -EnableAutomaticMigrations以生成Configuration类,如下所示:
internal sealed class Configuration : DbMigrationsConfiguration<ApplicationDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(ApplicationDbContext context)
{
}
}
Now you could either update the db context's constructor to migrate to latest version as follows:
现在,您可以更新db上下文的构造函数以迁移到最新版本,如下所示:
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
Database.SetInitializer(
new MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>());
this.Database.Initialize(true);
}
Or migrate to the latest version on application start up as follows:
或者在应用程序启动时迁移到最新版本,如下所示:
using (var context = new ApplicationDbContext())
{
Database.SetInitializer(
new MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>());
context.Database.Initialize(true);
}