简体   繁体   中英

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. The project is set up as follows:

项目概况

Namespaces are set according to folder structure: EF_Test, EF_Test.Model and EF_Test.ViewModel. The App displays data from the DB in a 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:

<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 . App.config looks like this:

<?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. When he launches the app it crashes at DataEntities.C_PKE_Test.Load(); within the ViewModel constructor. I'm loading the data in the ViewModel constructor as follows:

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:

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) ...

I suspect the complaint in the XAML editor and the crash on my teammate's user are connected. 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. Logging messages reveal that in this scenario the statement 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

Yet the table exists and we can both query it in 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 .

On the General Page, you should be able to set the default schema for his account. 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" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM