C# EF通用DAL组件中的连接字符串

C# EF通用DAL组件中的连接字符串,c#,entity-framework,sql-server-ce,prism,entity-framework-5,C#,Entity Framework,Sql Server Ce,Prism,Entity Framework 5,项目结构: <?xml version="1.0" encoding="utf-8"?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <configSections> <!-- For more information on

项目结构:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <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=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="MyRecordzContext" connectionString="metadata=res://*...
  </connectionStrings>
</configuration>  
PresentationFramework.dll!System.Windows.Application.Application()
Xz.Shell.exe!Xz.Shell.App.App()
Xz.Shell.exe!Xz.Shell.App.Main()
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args)
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state)
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state)
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart()
[Native to Managed Transition]
有一个modules文件夹,我需要添加数据库并使存储库就在那里, 其他模块和程序集也可以使用它。 新:将其更改为先前已验证的状态,即将配置文件放置在根应用程序中, 但仍然存在错误。 这篇文章完全经过编辑


数据库位置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <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=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="MyRecordzContext" connectionString="metadata=res://*...
  </connectionStrings>
</configuration>  
PresentationFramework.dll!System.Windows.Application.Application()
Xz.Shell.exe!Xz.Shell.App.App()
Xz.Shell.exe!Xz.Shell.App.Main()
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args)
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state)
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state)
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart()
[Native to Managed Transition]
位置:..\db\db.sdf(卖家的app.config上两层) App.Config文件

位置:modules\ModuleX\(以前)

新地点:壳牌\(主项目) 新版本:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <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=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="MyRecordzContext" connectionString="metadata=res://*...
  </connectionStrings>
</configuration>  
PresentationFramework.dll!System.Windows.Application.Application()
Xz.Shell.exe!Xz.Shell.App.App()
Xz.Shell.exe!Xz.Shell.App.Main()
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args)
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state)
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state)
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart()
[Native to Managed Transition]

我的直觉是:ModuleX是DAL项目,它是从另一个项目(可能是Web、Windows窗体或控制台应用程序)引用的,我们称之为Startup项目

如果是这种情况,则不使用DAL项目中的App.Config文件

有两种解决方案:

  • 您可以在启动项目中指定要考虑的连接字符串。这意味着您需要在以下位置添加
    标记:

    • web.config,如果启动项目是web应用程序
    • app.config,否则(例如WPF、控制台)
  • 或者,您可以在文件中定义此连接字符串,这将使其可用于在该特定计算机上运行的任何.NET应用程序


  • DAL项目中的连接字符串在运行时不使用。我想它只被EF设计师使用。

    起初我的想法与此完全相同,但我在一些代码中看到了一个异常,不幸的是,我不记得在哪里,但在该代码中主项目不知道。真的不知道怎么做。但是作为一个已证实的答案,把配置放在主目录中是可以的,但是这里的连接可能有其他简单的错误。请看我的帖子的“2起嫌疑犯”部分。他们说得对吗?在main中有.config,在DAL项目中有DB,我指定的地址是正确的吗?虽然我可以用几种方法测试一切,但一些经验丰富的提示可以缩短我在这篇文章上的时间。另外,我提到了WPF,所以不要考虑web。@Sypress错误,正如您在问题中所写的:“在应用程序配置文件中找不到名为‘MyRecordzContext’的连接字符串。”这就是我的答案让我们先排除这个错误,然后看看连接字符串是否正确OK,WPF就像Windows窗体一样-您需要将连接字符串放在app.config中