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

卡特 测试交流评论92字数 2444阅读模式
摘要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?

玩技站长微信
添加好友自动发送入群邀请
weinxin
rainbow-shownow
玩技官方公众号
官方微信公众号
weinxin
PLAYEZU
 
  • 版权提示:本站仅供存储任何法律责任由作者承担▷诈骗举报◁▷新闻不符◁▷我要投稿◁
    风险通知:非原创文章均为网络投稿真实性无法判断,侵权联系2523030730
    免责声明:内容来自用户上传发布或新闻客户端自媒体,切勿!切勿!切勿!添加联系方式以免受骗。
  • 原创转载:https://www.playezu.com/179294.html
    转载说明: 点我前往阅读>>>
匿名

发表评论

匿名网友
确定