2013-03-04 135 views
1

我已經從AdventureWorks數據庫中生成實體模型;現在我想刪除app.config中的連接字符串,並在運行時設置它。在Model1.Context.cs文件我已經chnaged構造函數在運行時設置實體框架連接字符串

public AdventureWorksEntities(string str) 
     : base("name=AdventureWorksEntities") 
    { 
     this.Database.Connection.ConnectionString = str; 
    } 

和Program.cs文件

EntityConnectionStringBuilder ecsb = new EntityConnectionStringBuilder(); 
     ecsb.Metadata = @"res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl"; 
     ecsb.Provider = @"System.Data.SqlClient"; 
     ecsb.ProviderConnectionString = 
      @"data source=.\sqlexpress;initial catalog=AdventureWorks;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"; 

     using (var ent = new AdventureWorksEntities(ecsb.ConnectionString)) 
     { 
      Console.WriteLine(ent.Database.Connection.ConnectionString); 
      var add = ent.Addresses; 
      foreach (var ad in add) 
      { 
       Console.WriteLine(ad.City); 
      } 


     } 
     Console.ReadKey(); 

現在它說,沒有發現元數據關鍵字。如何在運行時爲實體框架設置連接字符串?

回答

7

這是一個使用標準.aspx登錄信息在連接字符串中設置UserID和Password信息的示例。沒有連接字符串設置存儲在web.config或app.config文件中。

修改Model.Designer.cs頁面如下:

public partial class Entities : ObjectContext 
{ 
    #region Constructors 


    public static string getConStrSQL(string UID,string PWD) 
    { 

     string connectionString = new System.Data.EntityClient.EntityConnectionStringBuilder 
     { 
      Metadata = "res://*", 
      Provider = "System.Data.SqlClient", 
      ProviderConnectionString = new System.Data.SqlClient.SqlConnectionStringBuilder 
      { 
       InitialCatalog = "your_database_name", 
       DataSource = "your_server", 
       IntegratedSecurity = false, 
       UserID = UID,     
       Password = PWD,    
      }.ConnectionString 
     }.ConnectionString; 

     return connectionString; 
    } 

    /// <summary> 
    /// Initialize a new Entities object. 
    /// </summary> 
    public Entities(string UID,string PWD) 
     : base(getConStrSQL(UID,PWD), "Entities") 
    { 
     this.ContextOptions.LazyLoadingEnabled = true; 
     OnContextCreated(); 
    } 
    ...... 
在你的代碼

然後頁面背後:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.Mvc; 
using System.Web.Security; 


public partial class views_html_form : System.Web.UI.Page 
{ 
public void Page_Load() 
{ 
    if (currentUser() == null) 
    { 
     HttpContext.Current.Response.Redirect("~/login.aspx"); 
    } 
} 
public static MembershipUser currentUser() 
{ 
    MembershipUser currentUser = Membership.GetUser(); 
    return currentUser; 
} 

public static string UID() 
{ 
    string UID = currentUser().UserName; 
    return UID; 
} 
public static string PWD() 
{ 
    string PWD = currentUser().GetPassword(); 
    return PWD; 
} 
public static void SelectRecord() 
{ 
    YourModel.Entities db = new YourModel.Entities(UID(), PWD()); 
    var query = from rows in db.Table_Name orderby rows.ID select rows; 
    ..... 

就是這樣。不要搞亂.config文件。或者,您可以發送數據庫名稱,例如,以同樣的方式作爲參數。

2

我會去的東西,如:

public AdventureWorksEntities(string server, string databaseName, string user, string password) 
     :base(new System.Data.EntityClient.EntityConnectionStringBuilder 
     { 
      Metadata = "res://*", 
      Provider = "System.Data.SqlClient", 
      ProviderConnectionString = new System.Data.SqlClient.SqlConnectionStringBuilder 
      { 
       InitialCatalog = databaseName, 
       DataSource = server, 
       IntegratedSecurity = false, 
       UserID = user, 
       Password = password, 

      }.ConnectionString 
     }.ConnectionString) 
    { 

    } 
相關問題