简体   繁体   中英

Entity Framework fails if executed from pendrive or server

My app was created using .Net 4.0 Client Profile, WPF, EF 6.0, Code-First, 2 dataproviders (SQL Server CE and MySQL).

If I copy the debug/release folder to a local file path , it works fine.

But if a copy this folder to a pendrive or put in a server folder . The first time I need to create a DbContext or call a database initializer, its fails with the following error ( J: is my 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.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' 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' 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:\\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()

Just like the one described in this link

Here is my 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>

and here is my wpf app OnStartup , the app crash when calling 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>());

As I said at the beginning, the app runs fine if the folder is in a local file path, only fails if executed in a server folder or a pendrive folder

I had a similar problem. My program was created using WPF (targeting the .NET Framework 4), Visual Studio 2013 and my computer has .NET 4.5.2 on it. I could run the program perfectly fine and other machines that also had .NET 4.5.2 on it could run it perfectly fine but when I put it on another machine which had only .NET 4.0 on it, that is when I got the error you described. What I guess I am saying is that I think it has something to do with how the 4.5.2 framework emulates the 4.0 framework.

Anyways, I fixed the problem by simply removing the entire entityFramework section of the app config.

<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>

I just took the above part out of my code and left literally everything else--in the app config and in all other files--alone.

Make sure to leave your

<connectionStrings>
   ...
</connectionStrings>

section there.

I do realize you have more things in your entityFramework section than I do so I hope my solution will actually solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM