Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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.

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.

like image 621
Calin Leafshade Avatar asked Jun 01 '26 18:06

Calin Leafshade


2 Answers

I kept getting the same error in my unit testing project. I solved it by modifying the TestInitialize function by calling the CreateIfNotExist() function

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

        context = new MyAccessContext(connection);
        context.Database.CreateIfNotExists();
        service = new YourClass(context);
    }
like image 146
user8515 Avatar answered Jun 04 '26 11:06

user8515


Several methods. You can set your database initializer to 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

If your model is already up to date, just make sure you have automigrations disabled:

AutomaticMigrationsEnabled = false;
like image 43
Steve Greene Avatar answered Jun 04 '26 13:06

Steve Greene