我在Visual Studio中创建了一个ASP.NET MVC示例应用程序。我说像下面两类:在Entity Framework中更新没有迁移的数据库
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; }
}
,然后我添加了两个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();
}
}
但现在当我跑项目,我得到这个错误:
自创建数据库以来,支持“ApplicationDbContext”上下文的模型已更改
我只想知道是否有方法更新数据库而不使用迁移(Add-migrations
和update-database
)?
我认为你正在寻找DropCreateDatabaseIfModelChanges – Rise
您可以使用[数据库初始化](http://www.entityframeworktutorial.net/如Rise所示,代码优先/数据库初始化策略代码优先.aspx)。这些对于早期发展很方便,并有自己的种子方法。一旦获得了要保留的非种子数据,您可以切换到迁移。使用空初始化程序不会创建数据库对象 - 它会假定代码模型与数据库匹配(危险)。 –