ASP.NET Identity DbContext 混淆 [英] ASP.NET Identity DbContext confusion

查看:34
本文介绍了ASP.NET Identity DbContext 混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个默认的 MVC 5 应用程序在 IdentityModels.cs 中带有这段代码 - 这段代码用于默认模板的所有 ASP.NET Identity 操作:

A default MVC 5 App comes with this piece of code in IdentityModels.cs - this piece of code is for all the ASP.NET Identity operations for the default templates:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }
}

如果我使用带有实体框架的视图搭建一个新控制器并在对话框中创建一个新数据上下文...",我会为我生成这个:

If I scaffold a new controller using views with Entity Framework and create a "New data context..." in the dialog, I get this generated for me:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace WebApplication1.Models
{
    public class AllTheOtherStuffDbContext : DbContext
    {
        // You can add custom code to this file. Changes will not be overwritten.
        // 
        // If you want Entity Framework to drop and regenerate your database
        // automatically whenever you change your model schema, please use data migrations.
        // For more information refer to the documentation:
        // http://msdn.microsoft.com/en-us/data/jj591621.aspx

        public AllTheOtherStuffDbContext() : base("name=AllTheOtherStuffDbContext")
        {
        }

        public System.Data.Entity.DbSet<WebApplication1.Models.Movie> Movies { get; set; }

    }
} 

如果我使用 EF 搭建另一个控制器 + 视图,例如对于动物模型,这个新行将在 public System.Data.Entity.DbSet 下自动生成;电影 { 得到;放;} - 像这样:

If I scaffold another controller + view using EF, say for instance for an Animal model, this new line would get autogenerated right under public System.Data.Entity.DbSet<WebApplication1.Models.Movie> Movies { get; set; } - like this:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace WebApplication1.Models
{
    public class AllTheOtherStuffDbContext : DbContext
    {
        // You can add custom code to this file. Changes will not be overwritten.
        // 
        // If you want Entity Framework to drop and regenerate your database
        // automatically whenever you change your model schema, please use data migrations.
        // For more information refer to the documentation:
        // http://msdn.microsoft.com/en-us/data/jj591621.aspx

        public AllTheOtherStuffDbContext() : base("name=AllTheOtherStuffDbContext")
        {
        }

        public System.Data.Entity.DbSet<WebApplication1.Models.Movie> Movies { get; set; }
        public System.Data.Entity.DbSet<WebApplication1.Models.Animal> Animals { get; set; }

    }
} 

ApplicationDbContext(对于所有 ASP.NET Identity 内容)继承自 IdentityDbContext,后者又继承自 DbContext.AllOtherStuffDbContext(对于我自己的东西)继承自 DbContext.

ApplicationDbContext (for all the ASP.NET Identity stuff) inherits from IdentityDbContext which in turn inherits from DbContext. AllOtherStuffDbContext (for my own stuff) inherits from DbContext.

所以我的问题是:

这两个模型中的哪一个(ApplicationDbContextAllOtherStuffDbContext)应该用于我自己的所有其他模型?或者我应该只使用默认的自动生成的 ApplicationDbContext,因为使用它应该没有问题,因为它派生自基类 DbContext,还是会有一些开销?你应该在你的应用程序中为所有模型只使用一个 DbContext 对象(我已经在某处读过这个)所以我什至不应该考虑同时使用 ApplicationDbContextAllOtherStuffDbContext 在单个应用程序中?或者 MVC 5 中使用 ASP.NET Identity 的最佳实践是什么?

Which of these two (ApplicationDbContext and AllOtherStuffDbContext) should I use for all my other own models? Or should I just use the default autogenerated ApplicationDbContext since it shouldn't be a problem using it since it derives from the base class DbContext, or will there be some overhead? You should use only one DbContext object in your app for all your models (I've read this somewhere) so I should not even consider using both ApplicationDbContext and AllOtherStuffDbContext in a single app? Or what is best practice in MVC 5 with ASP.NET Identity?

推荐答案

我将使用从 IdentityDbContext 继承的单个 Context 类.通过这种方式,您可以让上下文了解您的类与 IdentityDbContext 的 IdentityUser 和角色之间的任何关系.IdentityDbContext 的开销很小,它基本上是一个带有两个 DbSet 的常规 DbContext.一种用于用户,一种用于角色.

I would use a single Context class inheriting from IdentityDbContext. This way you can have the context be aware of any relations between your classes and the IdentityUser and Roles of the IdentityDbContext. There is very little overhead in the IdentityDbContext, it is basically a regular DbContext with two DbSets. One for the users and one for the roles.

这篇关于ASP.NET Identity DbContext 混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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