为什么不能检查模型兼容性? [英] Why Model compatibility cannot be checked?
问题描述
然后我在de UI中尝试了一些测试,并得到这个消息:
EntityFramework.dll中出现类型System.NotSupportedException的异常,但未在用户代码中处理
附加信息:无法检查模型兼容性,因为
数据库不包含模型元数据。模型兼容性
只能使用Code First或Code First
迁移创建的数据库进行检查。
这是消息来自的代码。
public class TemInitializer:System.Data.Entity.DropCreateDatabaseIfModelChanges< ApplicationDbContext>
{
public override void InitializeDatabase(ApplicationDbContext context)
{
base.InitializeDatabase(context);
}
}
有人可以告诉我如何解决这个问题,需要查看?
初始化器删除并重新创建数据库 c>如果数据库模式不再匹配代码中的类。它通过查看你的类并通过查看数据库中名为 _MigrationHistory
的表来确定是否改变模型。如果没有这样的表,它会在问题中抛出异常。
所以要解决这个问题,你可以:
- 使用不同的初始化程序(例如
DropCreateDatabaseAlways
)或 - 确保有通过启用 _MigrationHistory >实体框架代码首次迁移。
I had to work further from a ex collega. So I run the asp.net solution without database. and it created a database. I think this is Code First or Code First Migrations.
Then I tried some tests in de UI and get this message:
An exception of type 'System.NotSupportedException' occurred in EntityFramework.dll but was not handled in user code
Additional information: Model compatibility cannot be checked because the database does not contain model metadata. Model compatibility can only be checked for databases created using Code First or Code First Migrations.
This is the code where de message come from.
public class TemInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<ApplicationDbContext>
{
public override void InitializeDatabase(ApplicationDbContext context)
{
base.InitializeDatabase(context);
}
}
Can anybody tell me how to solve this or where i need to look?
The DropCreateDatabaseIfModelChanges
initializer drops and recreates the database if the database schema no longer matches the classes in your code. It determines "if model changes" by looking at your classes and by looking at a table called _MigrationHistory
in the database. If there is no such table, it throws the exception in the question.
So to solve this, you can:
- Use a different initializer (such as
DropCreateDatabaseAlways
) or - Make sure that there is a
_MigrationHistory
table in your database, by enabling Entity Framework code first migrations.
这篇关于为什么不能检查模型兼容性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!