c# - 如果从 pendrive 或服务器执行 Entity Framework 失败

标签 c# wpf entity-framework ef-code-first app-config

我的应用是使用 .Net 4.0 Client Profile、WPF、EF 6.0、Code-First、2 个数据提供程序(SQL Server CE 和 MySQL)创建的。

如果我将 debug/release 文件夹复制到本地文件路径,它工作正常。

但是如果将此文件夹复制到pendrive 或放入服务器文件夹。 第一次我需要创建一个 DbContext或调用数据库初始化器,它失败并出现以下错误(J: 是我的 pendrive):

The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception An error occurred creating the configuration section handler for Entity Framework: Request failed.(J:\USERS\George\Release\app.Config line 6)

System.TypeInitializationException: The type initializer for 'System.Data.Entity.CreateDatabaseIfNotExists1' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception. ---> System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for entityFramework: Request failed. ---> System.Security.SecurityException: Request failed.<br/> at System.Void System.Data.Entity.CreateDatabaseIfNotExists1[DG.Apps.JLM.Questoes.Domain.Contexts.QuestoesContext]..ctor()
at System.Void DG.Apps.JLM.Questoes.Presentation.App.OnStartup(System.Windows.StartupEventArgs e) in j:\USERS\George\Release\Common\Common\Apps\JLM\DG.Apps.JLM.Questoes\DG.Apps.JLM.Questoes.Presentation\App.xaml.cs:line 30
at Object System.Windows.Application.<.ctor>b__1(System.Object unused)
at Object System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, System.Object args, System.Int32 numArgs)
at Object MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object source, System.Delegate method, System.Object args, System.Int32 numArgs, System.Delegate catchHandler)
at System.Void System.Windows.Threading.DispatcherOperation.InvokeImpl()
at static System.Void System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object state)
at static System.Void System.Threading.ExecutionContext.runTryCode(System.Object userData)
at static System.Void System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state)
at static System.Void System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean ignoreSyncCtx)
at static System.Void System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state)
at Object System.Windows.Threading.DispatcherOperation.Invoke()
at System.Void System.Windows.Threading.Dispatcher.ProcessQueue()
at IntPtr System.Windows.Threading.Dispatcher.WndProcHook(System.IntPtr hwnd, System.Int32 msg, System.IntPtr wParam, System.IntPtr lParam, System.Boolean handled)
at IntPtr MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd, System.Int32 msg, System.IntPtr wParam, System.IntPtr lParam, System.Boolean handled)
at Object MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object o)
at Object System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, System.Object args, System.Int32 numArgs)
at Object MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object source, System.Delegate method, System.Object args, System.Int32 numArgs, System.Delegate catchHandler)
at Object System.Windows.Threading.Dispatcher.InvokeImpl(System.Windows.Threading.DispatcherPriority priority, System.TimeSpan timeout, System.Delegate method, System.Object args, System.Int32 numArgs)
at IntPtr MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd, System.Int32 msg, System.IntPtr wParam, System.IntPtr lParam)
at System.Void System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame frame)
at static System.Void System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame frame)
at static System.Void System.Windows.Threading.Dispatcher.Run()
at Object System.Windows.Application.RunDispatcher(System.Object ignore)
at Int32 System.Windows.Application.RunInternal(System.Windows.Window window)
at Int32 System.Windows.Application.Run(System.Windows.Window window)
at Int32 System.Windows.Application.Run()
at static System.Void DG.Apps.JLM.Questoes.Presentation.App.Main()

就像这个link中描述的一样

这是我的 app.config :

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="System.Data.SqlServerCe.4.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient" />
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
      <remove invariant="System.Data.SqlServerCe.4.0" />
      <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
    </DbProviderFactories>
  </system.data>
  <connectionStrings>
    <add name="QuestoesContextSqlServerCE" connectionString="Data Source=Questoes.sdf" providerName="System.Data.SqlServerCE.4.0" />
    <add name="QuestoesContextMySQL" connectionString="server=localhost;User Id=root;password=root;Persist Security Info=True;database=questoes" providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client" />
  </startup>
</configuration>

这是我的 wpf 应用 OnStartup ,调用时应用程序崩溃 SetInitializer

namespace DG.Apps.JLM.Questoes.Presentation
{
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

        Database.SetInitializer(new CreateDatabaseIfNotExists<QuestoesContext>());

正如我在开头所说,如果文件夹位于本地文件路径中,应用程序运行良好,只有在服务器文件夹或 pendrive 文件夹中执行时才会失败

最佳答案

我遇到了类似的问题。我的程序是使用 WPF(针对 .NET Framework 4)、Visual Studio 2013 创建的,我的计算机上有 .NET 4.5.2。我可以完美地运行这个程序,其他也有 .NET 4.5.2 的机器也可以完美地运行它但是当我把它放在另一台只有 .NET 4.0 的机器上时,那是我得到错误的时候你描述。我想我想说的是,我认为这与 4.5.2 框架如何模拟 4.0 框架有关。

无论如何,我通过删除应用配置的整个 entityFramework 部分解决了这个问题。

<entityFramework>
   <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
   <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
   </providers>
</entityFramework>

我只是从我的代码中删除了上面的部分,并保留了其他所有内容——在应用程序配置和所有其他文件中——单独保留。

一定要留下你的

<connectionStrings>
   ...
</connectionStrings>

那里的部分。

我确实意识到您的 entityFramework 部分中的内容比我多,所以我希望我的解决方案能够真正解决您的问题。

关于c# - 如果从 pendrive 或服务器执行 Entity Framework 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21943013/

相关文章:

c# - Entity Framework AsNoTracking() 无限导航属性

c# - 无法从 ICollection 中删除最后一项

c# - 分析.NET代码的框架或工具

c# - 调用 Task.Delay() 并在几分钟后询问还剩多少时间

c# - 格式化长日期时间字符串以删除 T 字符

c# - 如何使特定行的 WPF 数据网格单元格只读?

c# - 如何反编译 ASP.NET/C# Web 应用程序

c# - 通过 View 模型传递信息

c# - 如何解析XML文件

entity-framework - Entity Framework 代码优先和多个程序集