替换自动创建的 ApplicationDbContext
Replacing automatically created ApplicationDbContext
我应该用免责声明作为开头,说我是 ASP.NET 开发的新手,并不真正了解数据库上下文,尽管花了最后一个小时阅读文档。当我构建 ASP.NET MVC 5 应用程序时,我选择了个人用户帐户身份验证。 Visual Studio 创建了一个名为 IdentityModels.cs
的文件,并在其中定义了一个 ApplicationUser
class 和一个 ApplicationDbContext
class.
我已经完成了一些开发工作,我的 CRUD 控制器使用 ApplicationDbContext
与数据库对话,方法是在每个控制器上设置私有 属性:
private ApplicationDbContext db = new ApplicationDbContext();
然后在控制器操作中,我会执行以下操作:
return View(db.Trains.ToList());
我想整理这个数据库上下文,但我需要先了解它。我的主要问题是:
- 我的整个应用程序只使用一个数据库上下文是否可以,就像我现在所做的那样?
- 我可以将
IdentityModels.cs
中定义的ApplicationDbContext
class替换为我自己定义的吗?
ApplicationDbContext
class 派生自 IdentityDbContext<ApplicationUser>
,这是否意味着我应该为 Visual Studio 提供的用户身份验证内容和我自己的代码提供单独的数据库上下文?
我认为我的最终目标是使用我自己的数据库上下文,称为 DatabaseContext
,然后在我的所有控制器继承自的基本控制器中使用它。然后我只有一个实例化数据库上下文的地方,而不是在每个控制器中。
谁知道呢,我可能想错了。每个人似乎都有自己喜欢的处理方式。
谢谢!
Is it okay to use just one database context for my entire application, like I'm doing now?
- 如果您决定直接从 UI 层访问数据库(这是一个单独的讨论),那没关系,因为
ApplicationDbContext
是您控制器的私有字段控制器根据请求创建和处理 - ApplicationDbContext
将根据请求创建和处理。
Can I replace the ApplicationDbContext class defined in IdentityModels.cs with my own?
- 你绝对可以做到。它用于创建一个
UserStore
,它在构造函数中接收 DbContext
作为参数,因此 this
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(new CustomDbContext("connection")));
会起作用。您仍然必须确保 ApplicationUser
是您自定义上下文中的实体。当然你也可以override/replaceApplicationUser
The ApplicationDbContext class derives from
IdentityDbContext, does that mean I should have
seperate database contexts for my user authentication stuff provided
by Visual Studio, and my own code?
默认情况下Asp.Net Identity 会为您生成一个新的数据库,并且ApplicationDbContext
配置为使用该数据库。您也可以将与身份验证相关的实体存储在任何其他数据库中,您只需要确保所有相关表 there.You 也可以扩展此数据库以包含您在应用程序中使用的其他表,因此您可以在所有地方使用相同的上下文。
P.S: ApplicationDbContext
不必实现 IdentityDbContext<ApplicationUser>
,扩展默认值 DbContext
也可以(如果您已经生成了 Db,则必须更新 it\use 代码迁移才能使以下内容正常工作):
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
我想您想创建自己的自定义身份验证数据库来进行登录身份验证。您需要逐步按照给定的说明进行操作。
1) 您需要创建自己的 class 来实现自定义身份验证。例如,我正在创建以下学生 class。您可以在 MVC 的 MODEL 部分创建此 class。
class student{
public int rollno{get;set;}
public string firstName{get;set;}
public string lastName{get;set;}
2) 现在您要使用学生 class 的 identityDbContext 创建您自己的 dbcontext。您可以像这样在 App_Start 部分中创建此 class。
public class ApplicationDbContext : IdentityDbContext<student>
{
public ApplicationDbContext() : base()
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
3) 现在您可以在控制器中使用它了,如下所示
public class HomeController : Controller
{
// GET: Home
public async Task<ActionResult> Index()
{
var context = new ApplicationDbContext(); // DefaultConnection
var store = new UserStore<student>(context);
var manager = new UserManager<student>(store);
var signInManager = new SignInManager<student, string>(manager,
HttpContext.GetOwinContext().Authentication);
var username = "Edward";
var email = "abc@gmail.com";
var password = "Your password";
var user = await manager.FindByEmailAsync(email);
if (user == null)
{
user = new student {
UserName = username,//username ,Email are getting from identityDbContext and the other three RollNO,FirstName and LastName are your own Custom members.so like this you can extend your any kind of data you like add or extend with IdentityDbContext.
Email = email,
RollNo = "15",
FirstName = "David",
LastName = "Kandel"
};
await manager.CreateAsync(user, password);
}
else
{
var result = await signInManager.PasswordSignInAsync(user.UserName, password, true, false);
if (result == SignInStatus.Success)
{
return Content("Hello, " +user.rollno + "" + user.FirstName + " " + user.LastName);
}
}
return Content("Hello, Index");
}
}
我应该用免责声明作为开头,说我是 ASP.NET 开发的新手,并不真正了解数据库上下文,尽管花了最后一个小时阅读文档。当我构建 ASP.NET MVC 5 应用程序时,我选择了个人用户帐户身份验证。 Visual Studio 创建了一个名为 IdentityModels.cs
的文件,并在其中定义了一个 ApplicationUser
class 和一个 ApplicationDbContext
class.
我已经完成了一些开发工作,我的 CRUD 控制器使用 ApplicationDbContext
与数据库对话,方法是在每个控制器上设置私有 属性:
private ApplicationDbContext db = new ApplicationDbContext();
然后在控制器操作中,我会执行以下操作:
return View(db.Trains.ToList());
我想整理这个数据库上下文,但我需要先了解它。我的主要问题是:
- 我的整个应用程序只使用一个数据库上下文是否可以,就像我现在所做的那样?
- 我可以将
IdentityModels.cs
中定义的ApplicationDbContext
class替换为我自己定义的吗? ApplicationDbContext
class 派生自IdentityDbContext<ApplicationUser>
,这是否意味着我应该为 Visual Studio 提供的用户身份验证内容和我自己的代码提供单独的数据库上下文?
我认为我的最终目标是使用我自己的数据库上下文,称为 DatabaseContext
,然后在我的所有控制器继承自的基本控制器中使用它。然后我只有一个实例化数据库上下文的地方,而不是在每个控制器中。
谁知道呢,我可能想错了。每个人似乎都有自己喜欢的处理方式。
谢谢!
Is it okay to use just one database context for my entire application, like I'm doing now?
- 如果您决定直接从 UI 层访问数据库(这是一个单独的讨论),那没关系,因为
ApplicationDbContext
是您控制器的私有字段控制器根据请求创建和处理 -ApplicationDbContext
将根据请求创建和处理。
Can I replace the ApplicationDbContext class defined in IdentityModels.cs with my own?
- 你绝对可以做到。它用于创建一个
UserStore
,它在构造函数中接收DbContext
作为参数,因此 this
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(new CustomDbContext("connection")));
会起作用。您仍然必须确保 ApplicationUser
是您自定义上下文中的实体。当然你也可以override/replaceApplicationUser
The ApplicationDbContext class derives from IdentityDbContext, does that mean I should have seperate database contexts for my user authentication stuff provided by Visual Studio, and my own code?
默认情况下Asp.Net Identity 会为您生成一个新的数据库,并且ApplicationDbContext
配置为使用该数据库。您也可以将与身份验证相关的实体存储在任何其他数据库中,您只需要确保所有相关表 there.You 也可以扩展此数据库以包含您在应用程序中使用的其他表,因此您可以在所有地方使用相同的上下文。
P.S: ApplicationDbContext
不必实现 IdentityDbContext<ApplicationUser>
,扩展默认值 DbContext
也可以(如果您已经生成了 Db,则必须更新 it\use 代码迁移才能使以下内容正常工作):
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
我想您想创建自己的自定义身份验证数据库来进行登录身份验证。您需要逐步按照给定的说明进行操作。 1) 您需要创建自己的 class 来实现自定义身份验证。例如,我正在创建以下学生 class。您可以在 MVC 的 MODEL 部分创建此 class。
class student{
public int rollno{get;set;}
public string firstName{get;set;}
public string lastName{get;set;}
2) 现在您要使用学生 class 的 identityDbContext 创建您自己的 dbcontext。您可以像这样在 App_Start 部分中创建此 class。
public class ApplicationDbContext : IdentityDbContext<student>
{
public ApplicationDbContext() : base()
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
3) 现在您可以在控制器中使用它了,如下所示
public class HomeController : Controller
{
// GET: Home
public async Task<ActionResult> Index()
{
var context = new ApplicationDbContext(); // DefaultConnection
var store = new UserStore<student>(context);
var manager = new UserManager<student>(store);
var signInManager = new SignInManager<student, string>(manager,
HttpContext.GetOwinContext().Authentication);
var username = "Edward";
var email = "abc@gmail.com";
var password = "Your password";
var user = await manager.FindByEmailAsync(email);
if (user == null)
{
user = new student {
UserName = username,//username ,Email are getting from identityDbContext and the other three RollNO,FirstName and LastName are your own Custom members.so like this you can extend your any kind of data you like add or extend with IdentityDbContext.
Email = email,
RollNo = "15",
FirstName = "David",
LastName = "Kandel"
};
await manager.CreateAsync(user, password);
}
else
{
var result = await signInManager.PasswordSignInAsync(user.UserName, password, true, false);
if (result == SignInStatus.Success)
{
return Content("Hello, " +user.rollno + "" + user.FirstName + " " + user.LastName);
}
}
return Content("Hello, Index");
}
}