2015-06-11 31 views
0

我已經創建了一個使用標識的註冊表單的Web表單。背後的形式調用的代碼看起來是這樣的:EntityFramework在哪裏獲取連接字符串到我的本地數據庫?

protected void CreateUser_Click(object sender, EventArgs e) 
{ 
    var userStore = new UserStore<IdentityUser>(); 
    var manager = new UserManager<IdentityUser>(userStore); 

    var user = new IdentityUser() { UserName = UserName.Text }; 
    IdentityResult result = manager.Create(user, Password.Text); 

    if (result.Succeeded) 
    { 
     StatusMessage.Text = string.Format("User {0} was created successfully!", user.UserName); 
    } 
    else 
    { 
     StatusMessage.Text = result.Errors.FirstOrDefault(); 
    } 
} 

而且我的網絡配置文件是這樣的:

<?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> 
    <system.web> 
     <authentication mode="Forms" /> 
     <roleManager enabled="true" /> 
     <compilation debug="true" targetFramework="4.5" /> 
     <httpRuntime targetFramework="4.5" /> 
    </system.web> 
    <runtime> 
     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
      <dependentAssembly> 
       <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" /> 
       <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
      </dependentAssembly> 
     </assemblyBinding> 
    </runtime> 
</configuration> 

我也有我的機器上安裝了MS SQL快。當我使用表單時,應用程序在SQLExpress中創建一個名爲DefaultConnection的數據庫。

我的問題是如何實體/身份/ .NET知道我的數據庫可言,因爲我沒有任何地方寫明確的連接字符串?

如果這是某種「約定優於配置」的功能,那麼我怎麼才能明確直接實體到不同的數據庫?

編輯:

我曾嘗試加入

<add name="MyConnection" 
    connectionString="[the connection string];TrustServerCertificate=False" 
    providerName="System.Data.SqlClient" />` 

的連接字符串和更新了我創建的用戶代碼:

... 
var connString = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString; 
var context = new System.Data.Entity.DbContext(connString); 

var userStore = new UserStore<IdentityUser>(context); 
var manager = new UserManager<IdentityUser>(userStore); 
... 

但這扔了InvalidOperationException以下消息:

Additional information: The entity type IdentityUser is not part of the model for the current context.

最後編輯:

發現瞭如何避免該異常,改變了這個:

var context = new System.Data.Entity.DbContext(connString);

成這樣:

var context = new Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext(connString);

回答

4

按照慣例本地數據庫會在您的本地SQL Server實例上創建。

請參見:Code First to a New Database - MSDN

按照約定的DbContext創建了一個數據庫,爲您服務。

  • 如果本地SQL Express實例可用,則代碼首先創建了數據庫,該實例(默認情況下與Visual Studio 2010一起安裝)
  • 如果SQL Express是不可用,則代碼首先會嘗試和使用的LocalDB(默認安裝與Visual Studio 2012)
  • 該數據庫導出場境的完全合格的名字命名

您可以通過使用顯式連接字符串來它在web.config,如:

<configuration> 
    <connectionStrings> 
    <add name="MyDBContext" 
     connectionString="Data Source=SQLServerAddress;Integrated Security=SSPI;Initial Catalog=yourdbName" 
     providerName="System.Data.SqlClient" /> 
    </connectionStrings> 
</configuration> 

這裏記得名稱更改爲在代碼擴展DbContext類。

因此,舉例來說,如果要擴展DbContext爲:

public class MyDBContext : DbContext 

然後使用MyDBContext作爲在配置連接字符串鍵。

或關閉當然你也可以通過在連接字符串中的構造:

namespace MyCodeFirstProject 
{  
    public class MyDBContext: DbContext  
    {   
     public MyDBContext() : 
      base("Data Source=SQLServerAddress;Integrated Security=SSPI;Initial Catalog=yourdbName") {} 
    } 
} 
+0

定製連接此方法適用於MVC的項目,但在這裏我使用的形式,所以我沒有繼承的DbContext類,所有這些都嵌入到Identity中。我通過NuGet將Identity添加到空表單網絡應用程序。我設法通過在名爲DefaultConnection的''部分中添加一個來編輯連接字符串。有沒有可能明確說出使用哪個連接字符串? –

+0

您可以通過構造函數傳遞連接,使用ConfigurationManager從web.config獲取連接字符串,並在傳遞給構造函數時使用 – Habib

+0

請參閱我在OP中的編輯。 –

相關問題