2014-06-19 97 views
5

有人可以幫助我理解以下兩種方法將連接字符串傳遞給DbContext實體框架帶連接字符串的DbContext構造函數

方法1:

public EWebDBContextEMS() : base("mainConnectionString") 
{ 
} 

和方法#2:

public EWebDBContextEMS() : base("name=mainConnectionString") 
{ 
} 

This article statesname=...會由設計師生成,但我用純DbContext代碼測試,它的工作原理也是如此。

這是一個DbContext構造函數的行爲嗎?並且在documentation中,它沒有提及name=對於連接字符串是可接受的。

非常感謝

回答

4

類的DbContext類的言論有充分的解釋。
總之:

  • 你的第一個例子可能會導致所謂的「mainConnectionString」創建數據庫。
  • 名= XXXX的名字

某些工具做投入的App.config條目你看起來的app.config進行的ConnectionStrings。
您鏈接的在線文檔明確指出幫助的位置。

在線求助說:

使用給定的字符串作爲名稱 或連接字符串數據庫的連接將被 給哪個構造一個新的上下文實例。請參閱課程註釋以瞭解如何使用它來創建 連接。

如果你去到類的言論,你會發現一個完整的解釋....

///    The connection to the database (including the name of the database) can be specified in several ways. 
///    If the parameterless DbContext constructor is called from a derived context, then the name of the derived context 
///    is used to find a connection string in the app.config or web.config file. If no connection string is found, then 
///    the name is passed to the DefaultConnectionFactory registered on the <see cref="T:System.Data.Entity.Database"/> class. The connection 
///    factory then uses the context name as the database name in a default connection string. (This default connection 
///    string points to .\SQLEXPRESS on the local machine unless a different DefaultConnectionFactory is registered.) 
///    Instead of using the derived context name, the connection/database name can also be specified explicitly by 
///    passing the name to one of the DbContext constructors that takes a string. The name can also be passed in 
///    the form "name=myname", in which case the name must be found in the config file or an exception will be thrown. 
///    Note that the connection found in the app.config or web.config file can be a normal database connection 
///    string (not a special Entity Framework connection string) in which case the DbContext will use Code First. 
///    However, if the connection found in the config file is a special Entity Framework connection string, then the 
///    DbContext will use Database/Model First and the model specified in the connection string will be used. 
///    An existing or explicitly created DbConnection can also be used instead of the database/connection name. 
///    A <see cref="T:System.Data.Entity.DbModelBuilderVersionAttribute"/> can be applied to a class derived from DbContext to set the 
///    version of conventions used by the context when it creates a model. If no attribute is applied then the 
///    latest version of conventions will be used. 
+0

謝謝您的回答,它能指出我錯過了那篇文章的確切說法。 – Jonathon

+0

「看到課堂講話」,「如果你去課堂講話...」 - - 定位這是不明顯的。你是怎麼找到它們的? – redwards510

+0

@ redwards510我使用Resharper dotpeek來反編譯源代碼。它可以免費安裝。這是一個免費軟件。但還有其他工具,如.net反射器 –

1

第一種方法可以讓你在運行時創建自己的完整的連接字符串,而不是在你的app.config或web.config文件命名連接字符串。

方法2在您的配置文件中使用了一個命名連接字符串。

的構造函數的DbContext接受一個連接字符串作爲參數:

使用方法1,以及一個處理一個連接字符串的建設部分類,你可以像在運行時建立一個連接字符串。 你可能會更好構建一個連接字符串,並傳遞一個給構造是這樣的:

我用類似:

// the model name in the app.config connection string (any model name - Model1?) 
private static string GetConnectionString(string model, settings) 
{ 
    // Build the provider connection string with configurable settings 
    var providerSB = new SqlConnectionStringBuilder 
    { 
     InitialCatalog = settings.InitialCatalog, 
     DataSource = settings.DataSource, 
     UserID = settings.User, 
     Password = settings.Password 
    }; 

    var efConnection = new EntityConnectionStringBuilder(); 
    // or the config file based connection without provider connection string 
    // var efConnection = new EntityConnectionStringBuilder(@"metadata=res://*/model1.csdl|res://*/model1.ssdl|res://*/model1.msl;provider=System.Data.SqlClient;"); 
    efConnection.Provider = "System.Data.SqlClient"; 
    efConnection.ProviderConnectionString = providerSB.ConnectionString; 
    // based on whether you choose to supply the app.config connection string to the constructor 
    efConnection.Metadata = string.Format("res://*/Model.{0}.csdl|res://*/Model.{0}.ssdl|res://*/Model.{0}.msl", model); ; 
    return efConnection.ToString(); 

} 
相關問題