将Asp.Net Mvc默认应用程序dbcontext类移动到另一个文件夹 [英] Moving Asp.Net Mvc default application dbcontext class to another folder

查看:130
本文介绍了将Asp.Net Mvc默认应用程序dbcontext类移动到另一个文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个ASP MVC网站.我正在为我的站点使用身份验证,因此我采用了默认模板中的默认身份验证方法.我现在在身份应用程序dbcontext类和针对我的应用程序的自定义dbcontext类之间完全混淆了.我对此有很多疑问.

I am building an asp MVC website. I am using authentication for my site so I took the default authentication method from the default template. Im now fully confused between the identity application dbcontext class and my custom dbcontext class for my application. I have many questions regarding this..

1)是否应该分别为每个dbcontext启用迁移?

1) Should I enable migration for each dbcontexts separately?

2)我可以将两个上下文的数据库都指向一个数据库吗? (我已经看到答案说可以做到.)这有什么问题吗?

2) Can I point the database of both the context to a single database? (I have seen answers saying it can be done..)Is there any problems for that?

3)我可以将Identitymodel.cs类从models文件夹移动到另一个数据库上下文驻留的自定义文件夹Databases吗?我看到命名空间仍然是dbcontext.models而不是dbcontext.databases

3)Can I move the Identitymodel.cs class from models folder to my custom folder named Databases where my another dbcontext resides? I saw the namespaces still remain dbcontext.models and not dbcontext.databases

有人可以回答我吗..我查了所有与此有关的问题仍无法消除我的疑惑.

Can someone answer me.. I have looked up all the questions related to this still can't clear my doubts.

推荐答案

1)否

2)是的,您可以执行此操作,但是最好在一个上下文中实现它. 你怎么能做到这一点?您的答案在这里: 创建一个类名称为ApplicationDbcontext:

2)yes you can do this but is better that you implement it in one context. how can you do this? your answer is here: create a class name of 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身份POCO:

create your custom Identity POCOs:

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

now you have one DbContext and can delete IdentiyMode.cs

执行以下命令:Add-Migration initUpdate-Database --verbose

您不需要其他DbContext

IdentityContext继承DbContext:

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

这篇关于将Asp.Net Mvc默认应用程序dbcontext类移动到另一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆