默认的MVC 5应用程序在IdentityModels中附带了这段代码.cs-这段代码适用于所有ASP.默认模板的NET标识操作:

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

如果我使用实体框架视图构建一个新的控制器,并创建一个"新的数据上下文…"在对话框中,我会生成以下内容:

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<WebApplication1.Models.Movie> Movies { get; set; }以下自动生成-如下所示:

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,而IdentityDbContext又继承自DbContext.

所以我的问题是:

这两个(ApplicationDbContextAllOtherStuffDbContext)中的哪一个应该用于我自己的所有其他模型?或者我应该只使用默认的autogenerated ApplicationDbContext,因为使用它应该不会有问题,因为它派生自基类DbContext,还是会有一些开销?你应该只在你的应用程序中为你的所有模型使用一个DbContext个对象(我读过这个地方),所以我不应该考虑在一个应用程序中同时使用ApplicationDbContextAllOtherStuffDbContext.或者什么是MVC5与ASP的最佳实践.网络身份?

推荐答案

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

Asp.net相关问答推荐

有人可以解释一下这个Eager 加载示例吗?

HttpRuntime.Cache[] 与 Application[]

如何使用 executeReader() 方法仅检索一个单元格的值

格式化 DataBinder.Eval 数据

某些黑客能否从用户那里窃取 Web 浏览器 cookie 并在网站上使用该名称登录?

如何在 IISExpress 上使用带有 localhost 的子域?

Anti-Forgery Token 适用于不同的基于声明的用户

在 Asp.net 中通过 Button 的 CommandArgument 传递多个参数

无法让嵌套在 UpdatePanel 中的 WebControl 中的 ScriptManager.RegisterStartupScript 工作

在 ASP.NET 中使用 Messenger Connect 客户端库时出现 Javascript 错误

如何在 ASP.Net 网络表单中使用标签?

ASP.NET IIS Web.config [内部服务器错误]

如何判断用户代理是 ipad 还是 iphone?

在 Android/Java 和 C# 中计算 SHA256 哈希

有条件地排除 ASP.NET WebForms 中的一个 html 块

使用 .NET 连接到 AS400

ASP.NET 发布try 复制不存在的文件

中继器中的中继器

ASP.NET 网格视图与列表视图

您将如何将 ASP.Net MVC 嵌入到现有的网站项目中?