模型和表更改错误 EF 4.1
我的 MVC3 应用程序中有以下模型,并且我首先使用实体框架 4.1 代码。
public class Movie
{
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Director { get; set; }
[CustomValidation(typeof(MovieValidator), "ValidateReleaseDate")]
public DateTime ReleaseDate { get; set; }
}
不,如果我从“标题”和“导演”中删除“必需”属性,并使它们在数据库中可为空(首先由上述模型中的 EF 代码自动创建),我会收到以下错误:
支持“MoviesDB”的模型 自数据库以来上下文已更改 被创建。要么手动 删除/更新数据库,或调用 Database.SetInitializer 具有 IDatabaseInitializer 实例。为了 例如, 如果模型更改则删除创建数据库 策略会自动删除和 重新创建数据库,并且可以选择 用新数据播种。
我已阅读过,要解决此问题,我必须执行以下操作之一:
1)删除数据库,以便 EF 可以重新创建它
2)在上下文类中添加以下行:(这在 EF 4.1 的上下文中不起作用,我在 application_start 中添加它有效。为什么它现在在上下文调用中不起作用?)
Database.SetInitializer<MoviesDB>(
new DropCreateDatabaseIfModelChanges<MoviesDB>());
3)将表与模型匹配(我的模型与表完全相同,为什么我仍然收到此错误?)
请建议第 2 点和第 3 点中提到的问题的答案。
谢谢。
I have following model in my MVC3 application and I am using entity frame 4.1 code first.
public class Movie
{
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Director { get; set; }
[CustomValidation(typeof(MovieValidator), "ValidateReleaseDate")]
public DateTime ReleaseDate { get; set; }
}
Noe, If I remove the Required attribute from Title and Director and make them nullable in database (which is automatically created by EF code first from the above model), I get following error:
The model backing the 'MoviesDB'
context has changed since the database
was created. Either manually
delete/update the database, or call
Database.SetInitializer with an
IDatabaseInitializer instance. For
example, the
DropCreateDatabaseIfModelChanges
strategy will automatically delete and
recreate the database, and optionally
seed it with new data.
I have read that to solve this I have to do one of the following things:
1) remove db so that EF can recreate it
2) add following lines in context class: ( this doesnt work in context with EF 4.1, I added in application_start and it works. Why doesnt it work in context calss now ?)
Database.SetInitializer<MoviesDB>(
new DropCreateDatabaseIfModelChanges<MoviesDB>());
3) match table with model ( my model is exactly same as table why I still get this error ?)
Please suggest answers of questions mentioned in point 2 and 3.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置初始化程序应该在上下文的构造函数中起作用,但不需要每次初始化上下文时都调用 - 您只需要执行一次,因此在应用程序启动期间调用它是一个好方法。
如果您使用自动更改检测(默认行为),则手动更新数据库不起作用。当上下文创建模型时,它会根据该模型计算哈希,并将其与
EdmMetadata
表(EF 第一次创建数据库时创建的表)中存储的值进行比较。如果从数据库加载的值不同,EF 要么启动数据库重新创建(如果配置了初始化程序),要么抛出您看到的异常。手动更改数据库不会修改存储在EdmMetadata
中的哈希值,因此模型仍会认为数据库未更改。可以通过删除
IncludeMetadataConvention
来删除此行为:删除此约定后,EF 将不会使用
EdmMetadata
表,也不会检查模型中的更改。更新数据库完全取决于您。Setting initializer should work for example in context's constructor but it is not needed to call every time you initialized context - you need to do it only once so calling it during application startup is a good approach.
Manully updating database doesn't work if you are using automatic change detection (default behavior). When context creates a model it computes a hash from this model and compares it with value stored in
EdmMetadata
table (table is created first time the EF creates the database). If the value loaded from database is different EF either starts database recreation (if initializer is configured) or throws the exception you see. Manually changing the database will not modify hash stored inEdmMetadata
so the model will still think that the database is unchanged.This behavior can be removed by removing
IncludeMetadataConvention
:After removing this convention EF will not use
EdmMetadata
table and will not check changes in the model. Updating the database will be completely up to you.