2015-12-14 13 views
0

m working for a company that they use dll for connection string and i中號tryng改變asp.net身份默認連接字符串中ApplicationDbContext類, 我使用的DLL連接類是這樣的:如何從dll文件設置asp.net標識連接字符串,而不是從webconfig?

public class HRConnection 
{  
    public string HR_con 
    { 
     get 
     { 
      return "server=192.168.1.21; database=HR; user=teamuser ; [email protected] ; connection timeout=30";     
     } 
    } 
} 

ve tried to pass string like this , but it didn噸工作

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 
    string HrConn = new HRConnection().HR_con; 
    public ApplicationDbContext() 
     : base(HrConn, throwIfV1Schema: false) 
    { 
    } 
} 

怎麼辦我在代碼中設置連接,而不是從webconfig文件中設置連接?

回答

0

我解決這樣

public ApplicationDbContext() 
     : base(new HRConnection().HR_con, throwIfV1Schema: false) 
    { 
    } 
0

首先我的問題,嘗試通過硬編碼字符串作爲第一個參數呼叫基地。如果這樣的作品,那麼我建議定義你這樣的字符串:

public class HRConnection 
{  
    public readonly static string HR_con = "server=192.168.1.21; database=HR; user=teamuser ; [email protected] ; connection timeout=30"; 
} 

,並使用這樣的:

public ApplicationDbContext() 
     : base(HRConnection.HR_con, throwIfV1Schema: false) 
    { 
    } 
+0

不妨把你的ConnectionString在基礎構造函數的參數硬編碼的,沒有點定義一個額外的類因爲它imo –

+0

@AlexanderDerck我同意你的意見。只是在連接字符串被用於代碼的其他部分的情況下。 – sachin

+0

它不會像這樣退出,硬編碼字符串用於測試,並且實際連接從dll返回字符串,以便像我這樣的開發人員無法訪問。 –

相關問題