wpf 在 app.config 中找不到实体框架连接字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20129769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't Find Entity Framework Connection String in app.config
提问by jporcenaluk
I have a catch for exceptions that writes the exception to a message box. The exception I keep running into says "System.InvalidOperationException: No connection string named 'NameOfEntities' could be found in the application config file. at..."
我有一个将异常写入消息框的异常捕获。我一直遇到的异常说“System.InvalidOperationException:在应用程序配置文件中找不到名为'NameOfEntities'的连接字符串。at...”
Following that, it notes that I'm trying to use LINQ to Entities on the entity on three specific lines in my code and that's where the error is originating from.
之后,它指出我正在尝试在我的代码中的三个特定行上的实体上使用 LINQ to Entities,这就是错误的来源。
On these lines, there is pretty simple LINQ statements like:
在这些行中,有非常简单的 LINQ 语句,例如:
IEnumerable<EntityObject> item = (from d in entityVariable.EntityObjects select d).AsEnumerable<EntityObject>();
I have tried it as an enumerable, as a list, it doesn't matter...still the error appears. The error can occur when I'm running it, when I'm building it, and when I'm just editing code.
我已经尝试将它作为一个可枚举的,作为一个列表,没关系......仍然出现错误。当我运行它时,当我构建它时,当我只是在编辑代码时,可能会发生错误。
I have the connection string named "NameOfEntities" in my config file, between the <connectionStrings>tags, which this post suggests.
我的配置文件中有一个名为“NameOfEntities”的连接字符串,位于<connectionStrings>标签之间,本文建议使用.
The .edmx lives in another project (a class library) in the solution. As the example linked to above illustrates, this shouldn't be a problem, and I've been developing with this system long before the error starting coming up.
.edmx 位于解决方案中的另一个项目(类库)中。正如上面链接的示例所示,这应该不是问题,并且在错误开始出现之前很久我就一直在使用该系统进行开发。
For as much info as possible, here's the full error. The formatting of the message is for legibility:
对于尽可能多的信息,这里是完整的错误。消息的格式是为了便于阅读:
"System.InvalidOperationException: No connection string named 'CnmcEntities' could be found in the application config file. at System.Data.Entity.Internal.LazyInternalConnection.get_ConnectionHasModel() at system.Data.Entity.Internal.LazyInternalContext.InitializeContext() at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) at System.Data.Data.Entity.Internal.Linq.InternalSet`1.Initialize() at System.Data.Entity.Internal.Linq.Set`.get_InternalContext() at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() at System.LInq.Queryable.Select[TSource,TResult](IQueryable` source, Expression`1 selector) at NameOfProject.Model.LiveData..ctor() in c:\PlaceOfTheProject\NameOfProject\Model\LiveData.cs:line 19 at NameOfProject.ViewModel.ChildViewModels.DataPointsViewModel..ctor(LiveDataViewModel thisMainView) in c:\PlaceOfTheProject\NameOfProject\ViewModel\LiveData\LiveDataViewModel\ChildViewModels\DataPointsViewModel.cs:line45 at NameOfProject.ViewModel.LiveDataViewModel.InitializeData() in c:\PlaceOfProject\NameOfProject\ViewModel\LiveDataViewModel\LiveDataViewModel.cs:line 103"
“System.InvalidOperationException:在应用程序配置文件中找不到名为‘CnmcEntities’的连接字符串。在 System.Data.Entity.Internal.LazyInternalConnection.get_ConnectionHasModel() 在 system.Data.Entity.Internal.LazyInternalContext.InitializeContext() 在System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) at System.Data.Data.Entity.Internal.Linq.InternalSet`1.Initialize() at System.Data.Entity.Internal.Linq.Set`.get_InternalContext () 在 System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() 在 System.LInq.Queryable.Select[TSource,TResult](IQueryable` 源,Expression`1 选择器) 在 NameOfProject。 Model.LiveData..ctor() in c:\PlaceOfTheProject\NameOfProject\Model\LiveData.cs:line 19 at NameOfProject.ViewModel。ChildViewModels.DataPointsViewModel..ctor(LiveDataViewModel thisMainView) 在 c:\PlaceOfTheProject\NameOfProject\ViewModel\LiveData\LiveDataViewModel\ChildViewModels\DataPointsViewModel.cs:line45 在 NameOfProject.ViewModel.LiveDataViewModel.InitializeData() 在 c:\PlaceOfProject\NameOfProject\ \LiveDataViewModel\LiveDataViewModel.cs:line 103"
Here's what I have (sans sensitive info) in the app.config:
这是我在 app.config 中的内容(无敏感信息):
<connectionStrings>
<add name="NameOfEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=SERVERSOURCE;initial catalog=NameOfInitialCatalog;persist security info=True;user id=UserId;password=Password;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<connectionStrings>
<add name="NameOfEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=SERVERSOURCE;initial catalog=NameOfInitialCatalog;persist security info=True;user id=UserId;password=Password;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
EDIT 1:
编辑 1:
As a "hot fix," instead of writing the exception to a message box, I am just swallowing the exception. There has been no ill effects on the application - it still accesses the data from the database. I hate this "solution," but as there seem to be no problems so far from swallowing the exception, I will keep it for now.
作为“热修复”,我没有将异常写入消息框,而是将异常吞下。对应用程序没有不良影响 - 它仍然访问数据库中的数据。我讨厌这个“解决方案”,但由于到目前为止似乎没有问题可以吞下异常,我暂时保留它。
Interestingly, I put a break point in the catch for the offending code, and it has not been hit yet after running the application several times. It seems like the exception was occurring most often on release of resources after closing the app, so maybe the debugger is no longer running when it hits this code. Either way, this has been a strange occurrence indeed.
有趣的是,我在捕获有问题的代码中放置了一个断点,并且在多次运行应用程序后还没有被命中。似乎异常最常发生在关闭应用程序后释放资源时,因此调试器在遇到此代码时可能不再运行。不管怎样,这确实是一个奇怪的事件。
I'm still open to any concrete ideas on how to fix the problem without swallowing the exception, or course.
我仍然愿意接受关于如何在不吞下异常或当然的情况下解决问题的任何具体想法。
回答by J.W.
Is this code in a library project, if so, you will need check connection string in web application , console, or windows application which is calling the library project.
此代码是否在库项目中,如果是,您将需要在调用库项目的 Web 应用程序、控制台或 Windows 应用程序中检查连接字符串。

