加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 02:00:15 所属栏目:百科 来源:网络整理
导读:我的应用程序是使用.Net 4.0 Client Profile,WPF,EF 6.0,Code-First,2 dataproviders(SQL Server CE和 MySQL)创建的. 如果我将调试/发布文件夹复制到本地文件路径,它可以正常工作. 但如果将此文件夹复制到pendrive或放入服务器文件夹中. 我第一次需要创建DbC
我的应用程序是使用.Net 4.0 Client Profile,WPF,EF 6.0,Code-First,2 dataproviders(SQL Server CE和 MySQL)创建的.

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

但如果将此文件夹复制到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:USERSGeorgeReleaseapp.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.
at System.Void System.Data.Entity.CreateDatabaseIfNotExists
1[DG.Apps.JLM.Questoes.Domain.Contexts.QuestoesContext]..ctor()
at System.Void DG.Apps.JLM.Questoes.Presentation.App.OnStartup(System.Windows.StartupEventArgs e) in j:USERSGeorgeReleaseCommonCommonAppsJLMDG.Apps.JLM.QuestoesDG.Apps.JLM.Questoes.PresentationApp.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.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.Object state,System.Boolean ignoreSyncCtx)
at static System.Void System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext,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.Boolean handled)
at Object MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object o)
at Object System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback,System.Delegate catchHandler)
at Object System.Windows.Threading.Dispatcher.InvokeImpl(System.Windows.Threading.DispatcherPriority priority,System.TimeSpan timeout,System.Int32 numArgs)
at IntPtr MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd,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,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,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框架有关.

无论如何,我通过简单地删除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部分有比我更多的东西,所以我希望我的解决方案能真正解决你的问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读