DropCreateDatabaseIfModelChanges EF6 results in System.InvalidOperationException: The model backing the context has changed

前端 未结 5 1185
后悔当初
后悔当初 2021-02-02 15:43

After migrating to Entity Framework 6 I get an error when executing unit tests on the build server.

I\'m using the DropCreateDatabaseIfModelChanges initiali

5条回答
  •  梦谈多话
    2021-02-02 16:08

    I found the same issue just after upgrading to EF6. After reading Stefan´s comment and having the same symptoms as he describes (tests were loading the Configuration class from my main project)

    Solution/workaround in my case was to

    • create new class TestContext: MyDataContext in my Tests project
    • change the initialiser from DropCreateDatabaseAlways to DropCreateDatabaseAlways
    • update/generalise places where I created my real context to use the test one

    I could do this because most of my tests just extend from a PersistenceTest class, so I understand this might be a pain to change if you have a big catalog. So looking forward to other solutions

提交回复
热议问题