简体   繁体   English

尝试使用Effort创建上下文时,Entity Framework 6会引发迁移异常

[英]Entity Framework 6 throws migration exception when trying to create a context with Effort

I'm trying to use Effort to test my application but if code-first migrations are enabled on the context then it throws the following exception: 我正在尝试使用Effort来测试我的应用程序但是如果在上下文中启用了代码优先迁移,那么它会抛出以下异常:

System.InvalidOperationException: Migrations is enabled for context 'XDbContext' but the database does not exist or contains no mapped tables. System.InvalidOperationException:为上下文“XDbContext”启用了迁移,但数据库不存在或不包含映射表。 Use Migrations to create the database and its tables, for example by running the 'Update-Database' command from the Package Manager Console. 使用迁移创建数据库及其表,例如通过从程序包管理器控制台运行“Update-Database”命令。

If I disable migrations then it all works fine. 如果我禁用迁移,那么一切正常。

How do I stop entity from even considering the migrations for the purposes of my tests. 为了我的测试目的,我如何阻止实体考虑迁移。

I kept getting the same error in my unit testing project. 我在单元测试项目中遇到了同样的错误。 I solved it by modifying the TestInitialize function by calling the CreateIfNotExist() function 我通过调用CreateIfNotExist()函数修改TestInitialize函数来解决它

    [TestInitialize]
    public void Initialize()
    {
        DbConnection connection = Effort.DbConnectionFactory.CreateTransient();

        context = new MyAccessContext(connection);
        context.Database.CreateIfNotExists();
        service = new YourClass(context);
    }

Several methods. 几种方法。 You can set your database initializer to null: 您可以将数据库初始化程序设置为null:

Database.SetInitializer<DatabaseContext>(null);

You can disable the initializer in web.config: https://msdn.microsoft.com/en-us/data/jj556606.aspx?f=255&MSPPError=-2147217396#Initializers 您可以在web.config中禁用初始化程序: https//msdn.microsoft.com/en-us/data/jj556606.aspx? f = 255 MSPPError = -2147217396#Initializers

If your model is already up to date, just make sure you have automigrations disabled: 如果您的模型已经是最新版本,请确保您已禁用自动版本:

AutomaticMigrationsEnabled = false;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM