簡體   English   中英

具有實體框架的WPF應用程序,app.config的正確設置

[英]WPF application with Entity Framework, proper settings for app.config

我有一個使用實體框架(數據庫優先方法)的WPF程序。 它在我的開發筆記本電腦上可以正常工作,但是在一開始在其他計算機上崩潰,這顯然是因為它有些無法連接到數據庫。

但是,關於問題的根本原因,我在網上閱讀了很多東西,盡管我沒有任何確定的答案,但是似乎它與連接字符串和app.config有關。

我在開發筆記本電腦上使用Microsoft SQL Server Management Studio,並且在測試單元上安裝了Microsoft SQL Server。

整個場景是我想發布我的應用程序,任何用戶都可以輕松下載並安裝它。

這是我的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>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
    <entityFramework>
        <defaultConnectionFactory 
              type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
            <parameters>
                <parameter value="mssqllocaldb" />
            </parameters>
        </defaultConnectionFactory>
        <providers>
             <provider invariantName="System.Data.SqlClient" 
                  type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
    </entityFramework>
    <connectionStrings>
        <add name="AssetManagementDBEntities"
             connectionString="metadata=res://*/Entity.AssetManagementDBModel.csdl|res://*/Entity.AssetManagementDBModel.ssdl|res://*/Entity.AssetManagementDBModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;initial catalog=AssetManagementDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;"
             providerName="System.Data.EntityClient"/>
    </connectionStrings>
</configuration>

我還必須說,我找不到csdlssdlmsl必須在其中的.dll文件!

.edmx文件的“生成操作”設置為EntityDeploy

最終,我找到了解決方案:

正如解釋在這里 ,這是不可能的,DB-第一種方法,因為它試圖每次初始化模式,因此它需要DB存在。 因此,我只是復制了基於數據庫創建的類和文件,並像代碼優先方法一樣使用它們(上面的鏈接對此進行了很好的說明)。 請注意,您應該完全刪除ConnectionString ,因為正如該鏈接中所述,DB上下文會處理該問題。

請注意,您的計算機上必須具有SQLEXPRESS或LocalDB (如果已安裝Visual Studio 2010或2012,則已經安裝其中之一)。

現在,程序首次運行時,它將在C:\\users\\[your_user_name]創建DB文件(應該有2個文件,即主DB文件和log文件)。

如果您對詳細信息有疑問,請發表評論

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM