我的應用程序是使用.Net 4.0客戶端配置文件,WPF,EF 6.0,Code-First,2數據提供程序(SQL Server CE和MySQL)創建的。如果從pendrive或服務器執行實體框架失敗
如果我將調試/發佈文件夾複製到本地文件路徑,它工作正常。
但是,如果將此文件夾複製到pendrive或放入服務器文件夾。 我第一次需要創建一個DbContext
或調用數據庫初始化,它的失敗,出現以下錯誤(J:
是我的U盤):
爲「System.Data.Entity.Internal.AppConfig」的類型初始引發了異常發生 錯誤創建用於實體框架配置節處理程序:請求失敗(Y: \ USERS \喬治\推出\ 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.
1 [DG.Apps.JLM.Questoes.Domain.Contex ts.QuestoesContext] .. ctor()
at System.Void System.Data.Entity.CreateDatabaseIfNotExists
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方法,System.Object參數,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)
在靜態System.Void System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext的ExecutionContext,System.Threading.ContextCallback回調,System.Object的狀態)
在靜態System.Void System.Threading.ExecutionContext.Run(系統。 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 handling)
at IntPtr MS.Win32.HwndWrapper.WndProc( System.IntPtr hwnd,System.Int32 msg,System.IntPtr wParam,System.IntPtr lParam,System.Boolean處理)
at Object MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object o)
at Object System.Windows。 Threading.ExceptionWrapper.InternalRealCall(System.Delegate回調,System.Object參數,系統。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味精,System.IntPtr wParam中,System.IntPtr LPARAM)
在System.Void System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame幀)
在靜態System.Void系統.Windows.Threading.Dis patcher.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()
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文件夾中執行時纔會失敗
你對此有什麼想法?看起來我有同樣的問題。 –
@PeterHansen不,但我帶來了一個替代解決方案,使用clickonce。 pendrive或服務器具有clickonce應用程序文件。當用戶從pendrive執行時,clickonce將文件複製到計算機中的另一個位置並從那裏執行,因此不會發生錯誤。 – George