2014-02-21 92 views
3

我的應用程序是使用.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.
at System.Void System.Data.Entity.CreateDatabaseIfNotExists
1 [DG.Apps.JLM.Questoes.Domain.Contex ts.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方法,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文件夾中執行時纔會失敗

+0

你對此有什麼想法?看起來我有同樣的問題。 –

+1

@PeterHansen不,但我帶來了一個替代解決方案,使用clickonce。 pendrive或服務器具有clickonce應用程序文件。當用戶從pendrive執行時,clickonce將文件複製到計算機中的另一個位置並從那裏執行,因此不會發生錯誤。 – George

回答

7

我有一個類似的問題。我的程序是使用WPF(面向.NET Framework 4)創建的,Visual Studio 2013和我的電腦上都有.NET 4.5.2。我可以很好地運行程序,其他機器也可以運行.NET 4.5.2,但是當我把它放在另一臺只有.NET 4.0的機器上時,那就是當我得到錯誤時描述。我想我說的是我認爲它與4.5.2框架模擬4.0框架有關。

無論如何,我通過簡單地刪除應用配置的整個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部分有更多的東西比我這樣做我希望我的解決方案能夠真正解決你的問題。

+0

嗨,這也適用於我!謝謝!但是..你知道爲什麼exxacly? –

+0

我從來沒有弄清楚它爲什麼修復它。抱歉。 – CatGuardian

+1

我真的很想知道這樣的編輯如何解決問題。它也適用於我,但我不明白。 – Matyas