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

Geo*_*rge 3 c# wpf entity-framework app-config ef-code-first

我的应用程序是使用.Net 4.0 Client Profile,WPF,EF 6.0,Code-First,2 dataproviders(SQL Server CE和MySQL)创建的.

如果我将调试/发布文件夹复制到本地文件路径,它可以正常工作.

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

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

System.TypeInitializationException: System.Void DG.Apps.JLM.Questoes.Presentation中'System.Data.Entity.CreateDatabaseIfNotExists 1 [DG.Apps.JLM.Questoes.Domain.Contexts.QuestoesContext] .. ctor()的类型初始值设定项.App.OnStartup(System.Windows.StartupEventArgs e)位于j:\ USERS\George\Release\Common\Common\Apps\JLM\DG.Apps.JLM.Questoes\DG.Apps.JLM.Questoes.Presentation\App. xaml.cs: Object System.Windows.Application的第30行.<.ctor> b__1(System.Object unused) 在Object System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback,System.Object args,System.Int32) numArgs) at System MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object source,System.Delegate方法,System.Object args,System.Int32 numArgs,System.Delegate catchHandler) System.Void System.Windows.Threading.DispatcherOperation 静态System.Void系统 上的静态System.Void System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object状态)中的.InvokeImpl().Threading.ExecutionContext.runTryCode(System.Object userData) 静态System.Void System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback回调,System.Object状态) 静态System.Void系统.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.Void的System System.Windows.Threading.DispatcherOperation.Invoke()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处理) 在IntPtr MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd,System.Int32 msg,System.IntPtr wParam,System.IntPtr lParam,System.Boolean得到) 对象MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object o) at Object System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback,System.Object)指定参数时,System.Int32 numArgs) 在对象MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object的源,System.Delegate方法,System.Object的指定参数时,System.Int32 numArgs,System.Delegate catchHandler) 在对象System.Windows. 在IntePtr MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd )中的Threading.Dispatcher.InvokeImpl(System.Windows.Threading.DispatcherPriority priority,System.TimeSpan timeout,System.Delegate方法,System.Object args,System.Int32 numArgs),系统.Int32消息,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.Threa ding.DispatcherFrame框架) 静态System.Void System.Windows.Threading.Dispatcher.Run() at Object System.Windows.Application.RunDispatcher(System.Object ignore) at Int32 System.Windows.Application.RunInternal(System.Windows.窗口窗口) 在Int32 System.Windows.Application.Run(System.Windows.Window窗口) 在Int32 System.Windows.Application.Run() 在静态System.Void DG.Apps.JLM.Questoes.Presentation.App.Main( ) 1' 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.
at System.Void System.Data.Entity.CreateDatabaseIfNotExists



























就像这个链接中描述的一样

这是我的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>
Run Code Online (Sandbox Code Playgroud)

这是我的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>());
Run Code Online (Sandbox Code Playgroud)

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

小智 7

我遇到了类似的问题.我的程序是使用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>
Run Code Online (Sandbox Code Playgroud)

我刚刚从我的代码中取出了上面的部分,并将其他所有内容 - 在app配置和所有其他文件中 - 单独留下.

一定要离开你的

<connectionStrings>
   ...
</connectionStrings>
Run Code Online (Sandbox Code Playgroud)

在那里.

我确实你在entityFramework部分有比我更多的东西,所以我希望我的解决方案能真正解决你的问题.