ASP.NET身份的DbContext混乱 [英] ASP.NET Identity DbContext confusion

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

问题描述

有一个默认的MVC应用5自带的这块code在IdentityModels.cs - 这块code是为默认模板所有的ASP.NET身份操作:

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脚手架另一个控制器+视图,例如说一个动物模型,这个新的生产线将获得在自动生成合适的公共System.Data.Entity.DbSet&LT; WebApplication1.Models.Movie&GT;电影{搞定;组; } - 是这样的:

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身份的东西)从 IdentityDbContext 继承反过来从<$继承C $ C>的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.

所以我的问题是:

这两个的( ApplicationDbContext AllOtherStuffDbContext )我应该用我所有自己的模式?或者我应该只使用自动生成默认的 ApplicationDbContext ,因为它不应该,因为它从基类派生使用它是一个问题的DbContext ,还是会有一定的开销?你应该只使用一个的DbContext 对象在你的应用程序为所有的模型(我读过这个地方),所以我不应该甚至考虑同时使用 ApplicationDbContext AllOtherStuffDbContext 在一个单一的应用程序?或者有什么是ASP.NET身份在5 MVC的最佳做法?

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继承。
这样你可以有上下文知道你的类和IdentityUser和IdentityDbContext的角色之间的关系。
有非常小的开销在IdentityDbContext,它基本上是一个普通的DbContext有两个DbSets。一个用于用户和一个用于作用

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身份的DbContext混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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