【问题标题】:Connection String in a EF general DAL assemblyEF 通用 DAL 程序集中的连接字符串
【发布时间】:2012-10-14 14:57:50
【问题描述】:

项目结构:

有一个模块文件夹,我需要在其中添加数据库并在其中创建存储库, 其他模块和程序集可以使用它。 新:将其更改为先前已证明的将配置文件放置在根应用程序中的状态, 仍然有错误。 这篇文章已完全编辑。


数据库位置:

location : ....\db\db.sdf(比卖家的app.config高2层)

App.Config 文件

位置:modules\ModuleX\(以前)

新位置:Shell\(主项目)

新版本:

<?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>  

2个嫌疑人: providerName="System.Data.EntityClient"

LocalDbConnectionFactory


错误信息:

错误行: TblMyRecord MyRecord = context.MyRecords.First(w => w.MyRecord == stMyRecord);

错误: 在应用程序配置文件中找不到名为“MyRecordzContext”的连接字符串。


目标:如何更改连接字符串,我的 app.config 以使应用程序按预期工作


这里使用了 EF 5.0、SQL CE、Prism、WPF、MVVM、C#


错误:

错误: 'System.Windows.Application' 的类型初始化程序引发了异常。

在应用程序运行时出现。

错误似乎出现在任何构建之前。

堆栈跟踪:

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]

【问题讨论】:

    标签: c# entity-framework sql-server-ce prism entity-framework-5


    【解决方案1】:

    这是我的预感:ModuleX 是 DAL 项目,它引用自另一个项目(可能是 Web、Windows 窗体或控制台应用程序),我们将其称为 Startup Project。

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

    有两种解决方案:

    1. 您可以在启动项目中指定连接字符串以将其考虑在内。这意味着您需要在以下位置添加&lt;connectionStrings&gt;...&lt;/connectionString&gt;标签:

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

    运行时不使用 DAL 项目中的连接字符串。我认为它仅供 EF 设计者使用。

    【讨论】:

    • 一开始我也是这么想的,但是我在一些代码中看到了一个异常,不幸的是我不记得在哪里,但是在那个代码中,主项目是不知道的。真的不知道怎么做。但是作为一个经过验证的答案,可以将配置放在主要位置,但这里的连接可能有其他简单的故障。请查看我帖子的“2 起嫌疑人案例”部分。他们是对的吗?在 main 中有 .config,在 DAL 项目中有 DB,我指定的地址是否正确?虽然我可以通过多种方式测试所有内容,但一些经验丰富的提示可以减少我在这件作品上花费的时间。
    • 我还提到了WPF,所以不要考虑网络。
    • @Sypress 错误,正如您在问题中所写的那样:“在应用程序配置文件中找不到名为 'MyRecordzContext' 的连接字符串。” 这就是导致我的回答
    • 我们先把这个错误去掉再看看连接字符串是否正确
    • 好的,WPF 就像 Windows 窗体一样 - 你需要将连接字符串放在 app.config 中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多