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

时间:2014-02-21 19:01:46

标签: 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)创建的。

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

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

  

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

     

System.TypeInitializationException:'System.Data.Entity.CreateDatabaseIfNotExists 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
1 [DG.Apps.JLM.Questoes.Domain.Contexts.QuestoesContext]的类型初始值设定项.. ctor()
  在System:Void DG.Apps.JLM.Questoes.Presentation.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:第30行   在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方法,System.Object args,System.Int32 numArgs,System.Delegate catchHandler)
  在System.Void System.Windows.Threading.DispatcherOperation.InvokeImpl()
  at static System.Void System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object state)
  在静态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)
  在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处理)
  在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 args,System.Int32 numArgs)
  at Object MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object source,System.Delegate方法,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)
  在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()
  在Object System.Windows.Application.RunDispatcher(System.Object ignore)
  在Int32 System.Windows.Application.RunInternal(System.Windows.Window窗口)
  在Int32 System.Windows.Application.Run(System.Windows.Window窗口)
  在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文件夹中执行时才会失败

1 个答案:

答案 0 :(得分: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>

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

请务必离开

<connectionStrings>
   ...
</connectionStrings>

那里的部分。

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