C# 如果从pendrive或服务器执行,实体框架将失败

C# 如果从pendrive或服务器执行,实体框架将失败,c#,wpf,entity-framework,ef-code-first,app-config,C#,Wpf,Entity Framework,Ef Code First,App Config,我的应用程序是使用.Net 4.0客户端配置文件、WPF、EF 6.0、代码优先、2个数据提供程序(SQL Server CE和MySQL)创建的 如果我将debug/release文件夹复制到一个本地文件路径,它可以正常工作 但如果用户将此文件夹复制到pendrive或放入服务器文件夹中。 第一次需要创建DbContext或调用数据库初始值设定项时,其失败并出现以下错误(J:是我的pendrive): “System.Data.Entity.Internal.AppConfig”的类型初始值设

我的应用程序是使用.Net 4.0客户端配置文件、WPF、EF 6.0、代码优先、2个数据提供程序(SQL Server CE和MySQL)创建的

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

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

“System.Data.Entity.Internal.AppConfig”的类型初始值设定项引发异常 为实体框架创建配置节处理程序时出错:请求失败。(J:\USERS\George\Release\app.Config第6行)

System.TypeInitializationException:“System.Data.Entity.CreateDatabaseIfNotExists
1”的类型初始值设定项引发异常。-->System.TypeInitializationException:“System.Data.Entity.Internal.AppConfig”的类型初始值设定项引发异常。-->System.Configuration.ConfigurationErrorsException:为entityFramework创建配置节处理程序时出错:请求失败。-->System.Security.SecurityException:请求失败。
在System.Void System.Data.Entity.CreateDatabaseIfNotExists上
1[DG.Apps.JLM.Questoes.Domain.context.QuestoesContext]…ctor()
在j:\USERS\George\Release\Common\Common\Apps\JLM\DG.Apps.JLM.Questoes\DG.Apps.JLM.Questoes\DG.Apps.JLM.Questoes.Presentation\App.xaml.cs第30行中的System.Void DG.Apps.JLM.Questoes.Presentation.App.OnStartup(System.Windows.StartupEventArgs)处 在对象System.Windows.Application.b__1(System.Object未使用)
在对象System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate回调,System.Object args,System.Int32 numArgs)
在对象MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object源、System.Delegate方法、System.Object args、System.Int32 numArgs、System.Delegate catchHandler)
位于System.Void System.Windows.Threading.DispatcherOperation.InvokeImpl()
在静态System.Void System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object状态)中
在静态System.Void System.Threading.ExecutionContext.runTryCode(System.Object userData)
在静态System.Void System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext ExecutionContext,System.Threading.ContextCallback回调,System.Object状态)
在静态System.Void System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext ExecutionContext,System.Threading.ContextCallback回调,System.Object状态,System.Boolean ignoreSyncCtx)
在静态System.Void System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext ExecutionContext,System.Threading.ContextCallback回调,System.Object状态)
在对象System.Windows.Threading.DispatcherOperation.Invoke()中 位于System.Void System.Windows.Threading.Dispatcher.ProcessQueue()
在IntPtr System.Windows.Threading.Dispatcher.WndProcHook(System.IntPtr hwnd,System.Int32 msg,System.IntPtr wParam,System.IntPtr lParam,System.Boolean handled)
在IntPtr MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd,System.Int32 msg,System.IntPtr wParam,System.IntPtr lParam,System.Boolean handled)
在对象MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object o)上
在对象System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate回调,System.Object args,System.Int32 numArgs)
在对象MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object源、System.Delegate方法、System.Object args、System.Int32 numArgs、System.Delegate catchHandler)
在对象System.Windows.Threading.Dispatcher.InvokeImpl(System.Windows.Threading.DispatcherPriority,System.TimeSpan超时,System.Delegate方法,System.Object参数,System.Int32 numArgs)
在IntPtr MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd,System.Int32 msg,System.IntPtr wParam,System.IntPtr lParam)
在System.Void System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame)中
在静态System.Void System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame)中
在静态System.Void System.Windows.Threading.Dispatcher.Run()中 在对象System.Windows.Application.RunDispatcher(System.Object忽略)
在Int32 System.Windows.Application.RunInternal(System.Windows.Window窗口)
在Int32 System.Windows.Application.Run(System.Windows.Window窗口)
在Int32 System.Windows.Application.Run()上 在static System.Void DG.Apps.JLM.Questoes.Presentation.App.Main()中

就像这本书中描述的一样

这是我的
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>

正如我在开始时所说,如果文件夹位于本地文件路径中,应用程序运行正常,只有在服务器文件夹或pendrive文件夹中执行时才会失败。我遇到了类似的问题。我的程序是使用WPF(针对.NET Framework 4)、Visual Studio 2013创建的,我的计算机上有.NET 4.5.2。我可以很好地运行这个程序,其他也有.NET 4.5.2的机器也可以很好地运行它,但是当我把它放在另一台只有.NET 4.0的机器上时,就是当我得到你描述的错误时。我想说的是,我认为这与4.5.2框架如何模拟4.0框架有关

无论如何,我只需删除app config的整个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部分比
<connectionStrings>
   ...
</connectionStrings>