简体   繁体   English

实体框架-在应用程序配置文件中找不到连接字符串

[英]Entity Framework - No connection string could be found in the application config file

I realize this question has been asked and answered before but the answers I've found don't quite apply to my problem. 我知道之前曾有人问过这个问题,但我发现的答案并不完全适用于我的问题。 I have one project only which is a WPF (MVVM) app using Entity Framework. 我只有一个项目 ,它是使用实体框架的WPF(MVVM)应用程序。 The project is set up as follows: 该项目的设置如下:

项目概况

Namespaces are set according to folder structure: EF_Test, EF_Test.Model and EF_Test.ViewModel. 命名空间是根据文件夹结构设置的:EF_Test,EF_Test.Model和EF_Test.ViewModel。 The App displays data from the DB in a DataGrid. 该应用程序在DataGrid中显示来自数据库的数据。 It compiles and runs fine when I start the app, even on a different machine. 当我启动应用程序时,即使在其他计算机上,它也可以编译并运行良好。

However, Visual Studio complains about the DataContext in XAML: 但是,Visual Studio抱怨XAML中的DataContext:

<Window x:Class="EF_Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:EF_Test"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:vm1="clr-namespace:EF_Test.ViewModel"
        mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Window.DataContext>
    <vm1:MainWindowViewModel/>
</Window.DataContext>
...

On <vm1:MainWindowViewModel/> it says No connection string ... could be found in the application config file . <vm1:MainWindowViewModel/>它说在应用程序配置文件中找不到连接字符串...。 App.config looks like this: App.config看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="BusinessEntities" connectionString="metadata=res://*/Model.EntityModel.csdl|res://*/Model.EntityModel.ssdl|res://*/Model.EntityModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=ursqldb-01;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

As I said, the project compiles and runs (for me). 正如我所说,该项目可以编译并运行(对我来说)。 My teammate however experiences a crash on startup. 但是我的队友在启动时遇到崩溃。 He has the exact same rights on the DB like me and Entity Framework is set up to use windows authentication for connecting to the DB. 他像我一样在数据库上拥有完全相同的权利,并且将实体框架设置为使用Windows身份验证连接到数据库。 When he launches the app it crashes at DataEntities.C_PKE_Test.Load(); 当他启动应用程序时,它在DataEntities.C_PKE_Test.Load();崩溃DataEntities.C_PKE_Test.Load(); within the ViewModel constructor. 在ViewModel构造函数中。 I'm loading the data in the ViewModel constructor as follows: 我将数据加载到ViewModel构造函数中,如下所示:

DataEntities = new BusinessEntities();
DataEntities.C_PKE_Test.Load(); // <- Crashes here only when my teammate runs the app
Collection.Source = DataEntities.C_PKE_Test.Local;
DataEntities.C_PKE_Test.Local.CollectionChanged += Local_CollectionChanged;
Collection.SortDescriptions.Add(new SortDescription("pke_id", ListSortDirection.Ascending));

The exception in my teammate's windows event viewer looks like this: 我队友的Windows事件查看器中的异常如下所示:

Description: The process terminated due to an unhandled exception. 说明:由于未处理的异常,进程终止了。 exception information: System.Data.SqlClient.SqlException at System.Data.SqlClient.SqlConnection.OnError(System.Data.SqlClient.SqlException, Boolean, System.Action'1) at System.Data.SqlClient.SqlInternalConnection.OnError(System.Data.SqlClient.SqlException, Boolean, System.Action'1) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(System.Data.SqlClient.TdsParserStateObject, Boolean, Boolean) at System.Data.SqlClient.TdsParser.TryRun(System.Data.SqlClient.RunBehavior, System.Data.SqlClient.SqlCommand, System.Data.SqlClient.SqlDataReader, System.Data.SqlClient.BulkCopySimpleResultSet, System.Data.SqlClient.TdsParserStateObject, Boolean ByRef) ... 异常信息:System.Data.SqlClient.SqlConnection.OnError上的System.Data.SqlClient.SqlException(System.Data.SqlClient.SqlInternalConnection.OnError(System.Data.SqlClient.SqlConnection.OnError(System.Data.SqlClient.SqlException,Boolean,System.Action'1)。 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(System.Data.SqlClient.TdsParserStateObject,Boolean,Boolean)处的Data.SqlClient.SqlException,布尔值,System.Action'1)。 Data.SqlClient.RunBehavior,System.Data.SqlClient.SqlCommand,System.Data.SqlClient.SqlDataReader,System.Data.SqlClient.BulkCopySimpleResultSet,System.Data.SqlClient.TdsParserStateObject,布尔ByRef)...

I suspect the complaint in the XAML editor and the crash on my teammate's user are connected. 我怀疑XAML编辑器中的投诉与队友的用户崩溃有关。 When I set the DataContext in code behind, the app doesn't crash but the DataGrid doesn't display any data when my teammate runs the app. 当我在后面的代码中设置DataContext时,应用程序不会崩溃,但当我的队友运行该应用程序时,DataGrid不会显示任何数据。 Logging messages reveal that in this scenario the statement DataEntities.C_PKE_Test.Load(); 记录消息显示,在这种情况下,语句DataEntities.C_PKE_Test.Load(); throws an exception: An error occurred while executing the command definition. 引发异常: 执行命令定义时发生错误。 See the inner exception for details. 有关详细信息,请参见内部异常。 And an inner exception: Invalid object name 'dbo._PKE_Test 还有一个内部异常: 无效的对象名称'dbo._PKE_Test

Yet the table exists and we can both query it in SQL Management Studio. 该表仍然存在,我们都可以在SQL Management Studio中查询它。 Does anyone have an idea what could be wrong here? 有谁知道这里可能有什么问题吗?

This may have to do with the schema your teammate is assigned to. 这可能与队友分配给的架构有关。

Go to the Security node of the database in question in SQL server Management Studio, expand it, right click on your teammate's account and hit Properties . 转到SQL Server Management Studio中有问题的数据库的“ Security节点,将其展开,右键单击您队友的帐户,然后单击“ Properties

On the General Page, you should be able to set the default schema for his account. General页面上,您应该能够为其帐户设置默认架构。 Make sure it matches yours. 确保它与您的匹配。

尝试在连接字符串中将数据库的名称指定为Initial Catalog

<add name="BusinessEntities" connectionString="metadata=res://*/Model.EntityModel.csdl|res://*/Model.EntityModel.ssdl|res://*/Model.EntityModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=ursqldb-01;Initial Catalog=YOURDATABASENAME;Integrated Security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有实体框架的ASP.NET Core 2,在应用程序配置文件中找不到名为“ ####”的连接字符串 - ASP.NET Core 2 with Entity Framework, showing no connection string named '####' could be found in the application config file 在应用程序配置文件中找不到名为“ KgSoftProContext”的连接字符串 - No connection string named 'KgSoftProContext' could be found in the application config file 在应用程序配置文件中找不到连接字符串 - EF6 - No connection string could be found in the application config file - EF6 InvalidOperationException:在应用程序配置文件中找不到名为“PayMyRentEntities”的连接字符串 - InvalidOperationException: No connection string named 'PayMyRentEntities' could be found in the application config file 在应用程序配置文件中找不到名为“ CREntities”的连接字符串 - No connection string named 'CREntities' could be found in the application config file 在应用程序配置文件中找不到名为“thenameofmyconnection”的连接字符串 - No connection string named “thenameofmyconnection” could be found in the application config file 在应用程序配置文件中找不到名为“MyEntities”的连接字符串 - No connection string named 'MyEntities' could be found in the application config file '在应用程序配置文件中找不到名为'MyEntities'的连接字符串。' - 'No connection string named 'MyEntities' could be found in the application config file.' 在应用程序配置文件中找不到已命名的连接字符串 - No connection string named could be found in the application config file 在配置文件中找不到连接字符串。 但它在那里 - No connection string could be found in the config file. But it is there
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM