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

asp.net-mvc effort entity-framework-6

Question

When code-first migrations are allowed on the context, Effort throws the following exception when I try to use it to test my application:

System.InvalidOperationException: Migrations is enabled for context 'XDbContext' but the database does not exist or contains no mapped tables. Use Migrations to create the database and its tables, for example by running the 'Update-Database' command from the Package Manager Console.

It all functions properly if I disable migrations.

How can I prevent the entity from taking the migrations into account during my tests?

1
3
1/10/2016 8:25:25 AM

Popular Answer

In my unit testing project, I often encountered the same issue. I fixed it by adding a call to the CreateIfNotExist() method in the TestInitialize function.

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

        context = new MyAccessContext(connection);
        context.Database.CreateIfNotExists();
        service = new YourClass(context);
    }
6
2/29/2016 3:34:10 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow