2017-08-11 79 views
-1

我正在構建一個asp MVC網站。我正在爲我的網站使用身份驗證,因此我使用默認模板的默認身份驗證方法。我現在完全混淆了身份應用程序dbcontext類和我的應用程序的自定義dbcontext類。我有很多關於這方面的問題。將Asp.Net Mvc默認應用程序dbcontext類移動到另一個文件夾

1)我應該爲每個dbcontexts分別啓用遷移嗎?

2)我可以將上下文的數據庫指向單個數據庫嗎? (我看到答案說可以完成..)有什麼問題嗎?

3)我可以將Identitymodel.cs類從models文件夾移動到我的另一個dbcontext所在的名爲Databases的自定義文件夾中嗎?我看到命名空間仍然是dbcontext.models而不是dbcontext.databases

有人可以回答我..我查過了與此相關的所有問題仍然無法清除我的疑惑。

回答

0

所以如果你真的需要兩個DbContexts(我很好奇爲什麼),你必須爲每個dbcontexts分別啓用遷移。您必須爲每個要生成的Configuration.cs文件指定一個文件夾。事情是這樣的:

PM> Enable-Migrations -ContextTypeName ApplicationDbContext -MigrationsDirectory Migrations\ApplicationDbContext 
Checking if the context targets an existing database... 
Code First Migrations enabled for project SomeWebApplication. 

PM> Enable-Migrations -ContextTypeName YourCustomDbContext -MigrationsDirectory Migrations\YourCustomDbContext 
Checking if the context targets an existing database... 
Code First Migrations enabled for project SomeWebApplication. 

你的上下文可以指向同一個分貝(他們可以有一個共同的連接字符串),只要不涉及做同桌。

是的,你可以移動IdentityModel到另一個文件夾,但你必須手動改變命名空間。 (或者讓ReSharper的,它可以自動進行)

+0

「所以,如果你真的需要有兩個DbContexts(我很好奇爲什麼)「..我不熟悉從2個dbcontexts製作單個的dbcontext。作爲一個腳手架,恐怕如果這可能會引發錯誤。 – Abhijith

1

1)無

2)是的,你可以做到這一點,但你實現它在一個環境更好。 你怎麼能這樣做?你的答案就在這裏: 創建的ApplicationDbcontext類名:

ApplicationDbcontext.cs

public class ApplicationDbContext :IdentityDbContext<User, Role, Guid, UserLogin, UserRole, UserClaim> 
{ 
    public ApplicationDbContext() : 
     base("Server=.;Initial Catalog=TestDb;Integrated Security=true;MultipleActiveResultSets=True;") 
    { 
    } 

    public DbSet<Person> Peoples { get; set; } 
    // add poco classes here 
} 

創建custom身份波蘇斯:

public class User : IdentityUser<Guid, UserLogin, UserRole, UserClaim> 

public class Role : IdentityRole<Guid,UserRole> 

public class UserLogin : IdentityUserLogin<Guid> 

public class UserRole : IdentityUserRole<Guid> 

public class UserClaim : IdentityUserClaim<Guid> 

現在你有一個的DbContext和可刪除IdentiyMode.cs

ex ecute這個命令:Add-Migration initUpdate-Database --verbose

你不需要其他的DbContext

IdentityContext繼承DbContext

public class IdentityDbContext<TUser, TRole, TKey, TUserLogin, TUserRole, 
      TUserClaim> : DbContext 
+0

仍然我會有2個上下文..一個我爲我的模型dbsets創建,一個來自IdentityDbContext。 – Abhijith

+0

你不需要你的上下文,只需在'ApplicationDbContext.cs'中定義'Dbset ',你就可以刪除'IdentiyModel。cs' –

+0

哎呀!我想你沒有得到我。我有2個上下文.. 1)公共類AstroEntities:DbContext爲我的模型2)公共類ApplicationDbContext:IdentityDbContext ..所以我問我是否可以將其添加在相同的文件夾或相同的命名空間 – Abhijith

相關問題