Database.SetInitializer(null) does not actually set it to null

卡特 测试交流评论144字数 400阅读1分20秒阅读模式
摘要after googling and trying and failing for hours I'm resulting to stackoverflow with this i...

after googling and trying and failing for hours I'm resulting to stackoverflow with this issue in hopes that someone else has encountered this.

I am trying to write an integration test using MSTest for a C# project. In order to perform the integration test I need to connect to a database, we're doing database first, not code first with EntityFramework, and in all our projects we use Database.SetInitializer(null);文章源自玩技e族-https://www.playezu.com/179294.html

However, when trying to debug my test I keep receiving this exception:文章源自玩技e族-https://www.playezu.com/179294.html

The model backing the 'ApplicationDbContext' context has changed since the database was created. This could have happened because the model used by ASP.NET Identity Framework has changed or the model being used in your application has changed. To resolve this issue, you need to update your database. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=301867). Before you update your database using Code First Migrations, please disable the schema consistency check for ASP.NET Identity by setting throwIfV1Schema = false in the constructor of your ApplicationDbContext in your application.
public ApplicationDbContext() : base("ApplicationServices", throwIfV1Schema:false)文章源自玩技e族-https://www.playezu.com/179294.html

on the last line of this code block:文章源自玩技e族-https://www.playezu.com/179294.html

string connectionString = @"Data Source=MyDataSource;Initial Catalog=MyCatalog;User Id=MyUsername; Password=MyPassword;MultipleActiveResultSets=True; Trusted_Connection=False;Persist Security Info=True";
System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connectionString);
connection.Open();
using (MyContext context = new MyContext(connection))

MyContext has a constructor override that looks like this:文章源自玩技e族-https://www.playezu.com/179294.html

public MyContext(SqlConnection connection): base(connection.ConnectionString)

This exception of course indicates that it is unable to migrate the database, which it is not supposed to. The database is used across several projects, so Code First would not be optimal in this case.文章源自玩技e族-https://www.playezu.com/179294.html

I have tried setting文章源自玩技e族-https://www.playezu.com/179294.html

Database.SetInitializer<MyContext>(null);

In the following places:文章源自玩技e族-https://www.playezu.com/179294.html

  • The start of the Integration test method.
  • In the constructor of MyContext.
  • In an initializer method decorated with [AssemblyInitialize]

Neither of these seem to make any difference.文章源自玩技e族-https://www.playezu.com/179294.html

I have tried using .NET framework 6.0, 5.0 and 4.7.1 (the project that I am testing is on 4.7.1).文章源自玩技e族-https://www.playezu.com/179294.html

I have the following Nu.Get packages isntalled in the test project:

  • ADO.Net.Client @ 1.4.3
  • coverlet.collector @ 3.0.2
  • EntityFramework @ 6.2.0
  • Microsoft.AspNet.Identity.Core @2.2.3
  • Microsoft.AspNet.Identity.EntityFramework @ 2.2.3
  • Microsoft.NET.Test.Sdk @ 17.2.0
  • MSTest.TestAdapter @ 2.2.10
  • MSTest.TestFramework @ 2.2.10
  • System.Configuration.ConfigurationManager @ 6.0.0
  • System.Data.SqlClient @ 4.8.3

Has anyone ever come across this issue - and how did you solve it?

 
匿名

发表评论

匿名网友
确定

拖动滑块以完成验证