c# - 为 EF 使用本地数据库副本 - "The specified named connection is either not found in the configuration"

标签 c# asp.net asp.net-mvc entity-framework

我正在尝试创建数据库架构的副本,然后在应用程序中临时使用它进行测试,这样我就不会更改实时数据。但是,无论我做什么,它总是给我这个错误:

An exception of type 'System.ArgumentException' occurred in System.Data.Entity.dll but was not handled in user code

Additional information: The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

我试过使我的连接字符串如下所示:

<add name="EntitiesConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\vipertest.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />

<add name="EntitiesConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\vipertest.mdf;Integrated Security=SSPI;User Instance=True" providerName="System.Data.SqlClient"/>

我试过使用 mdf 文件,但没有。

我在 Web.Config 和 app.config 中都这样做了,但我总是得到同样的错误。

最佳答案

Entity Framework 连接字符串通常看起来不像您显示的任何一个字符串。您的连接字符串看起来像一个普通的 sql server 连接字符串。

EF 连接字符串通常包含 EF 元数据。这是我的一个 EF 连接字符串中的示例:

所以你的连接字符串可能看起来有些不同,但它应该包含实体元数据引用等。

我会从生产中使用的实际连接字符串开始(如果你能得到它),甚至进行清理,然后相应地修改它。

This MSDN article on EF connection strings可能有帮助。

关于c# - 为 EF 使用本地数据库副本 - "The specified named connection is either not found in the configuration",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36607117/

相关文章:

c# - 如何获取进程的错误信息?

C# Excel 互操作 - 如何测试互操作对象是否仍在工作并执行任务?

c# - 如何在不关闭应用程序的情况下关闭主窗体?

c# - 使用 Exchange 服务和 OAuth 订阅推送通知时出现 401 Unauthorized

asp.net-mvc - 渗透测试您的 MVC 应用程序

c# - C#获取上一页URL的方法

c# - 在 ASP.NET 中将表格导出到 Word 文档时图像损坏

c# - 如何序列化存储在 session 中的 ListItem

c# - 执行 Ajax 时出现 500 内部服务器错误

asp.net-mvc - MVCContrib,Html.Grid : How can I attach a row-based id to a td tag?