0

如何在實體框架中使用應該位於解決方案目錄(或其他地方)中的mdf文件? 默認情況下,EF在「C:\ Program Files \ Microsoft SQL Server \ MSSQL10_50.SQLEXPRESS \ MSSQL \ DATA」中創建該文件。 此外,在啓動初始化服務器實例的.net應用程序之後,能夠通過Windows對等網絡訪問數據庫將會很好。在使用EF時使用附加文件的SQL Server Express

我想我必須使用類似的連接字符串的東西是: http://msdn.microsoft.com/en-us/library/bb264564%28v=sql.90%29.aspx,它採用 用戶實例和AttachDbFilename

這篇文章指出,我可以使用連接字符串像往常一樣與EF: http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-configuration-file-settings.aspx

當我使用我自己的(不是默認的EF)連接字符串時得到的是:'System.Data.Entity.Internal.AppConfig'的類型初始值設定項引發異常。 (配置系統初始化失敗)使用

<configuration> 
    <connectionStrings> 
    <add name="SomeContext" 
     providerName="System.Data.SqlClient" 
     connectionString="Server=.\SQLEXPRESS; 
     AttachDbFilename=C:\temp\SomeNamespace.SomeContext.mdf; 
     Integrated Security=True; 
     User Instance=True" /> 

    </connectionStrings> 
... 

版本:

的SQL Server Express版R2 SP1

EF 4.3.1(代碼優先)

回答

0

多虧了這些問題的答案,我能夠使用自定義位置:

Problem connecting to database - user instance and Entity Framework issue

WPF and ADO.NET EF - error part II

我的步驟是:

  • 複製.mdf和由EF被創造的地方我想他們是.LDF文件
  • 啓動Microsoft SQL Server Management Studio中(快遞)作爲管理員並使用SSMS(已被複制)刪除舊數據庫
  • 將複製的.mdf文件附加到使用SSMS的本地服務器
  • 重構App.config,以便configSections在connectionStrings部分之前
  • 更改連接字符串(它不使用的用戶實例不再被棄用反正)

我的App.config現在看起來是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections>  
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=u57a3c561234h089" /> 
    </configSections> 

    <connectionStrings> 
    <add name="SomeContext" 
     providerName="System.Data.SqlClient" 
     connectionString="Server=.\SQLEXPRESS; Integrated Security=True; Database=SomeNamespace.SomeContext" /> 
    </connectionStrings> 

    <entityFramework> 
    <defaultConnectionFactory 
    ... 
相關問題