EF6 ObjectMaterialized在测试中导致SqlExceptions

时间:2017-10-17 09:19:47

标签: entity-framework-6

订阅ObjectMaterialize

((IObjectContextAdapter)context).ObjectContext.ObjectMaterialized += (sender, e) => { /* Note, fails even when the handler is empty */ }

这在生产代码中运行良好。但是,只要我添加这个,我就会进入测试例外:

Message: System.Data.SqlClient.SqlException : A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

删除该单行(ObjectMaterialized + = ..)会再次解析它。我一直在调试,但找不到原因。这里有没有人碰巧遇到过这个?

1 个答案:

答案 0 :(得分:0)

我是一个doofus ...这是在没有ConnectionString的测试程序集中,显然添加此处理程序会导致在实例化DbContext时建立连接。

因此,如果您遇到此问题:将连接字符串添加到测试项目的App.config中。