首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ASP.NET Identity DbContext混淆

ASP.NET Identity DbContext混淆
EN

Stack Overflow用户
提问于 2013-11-11 17:21:43
回答 2查看 138.5K关注 0票数 205

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

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

如果我使用带有实体框架的视图搭建了一个新控制器,并创建了一个"New data context...“在对话框中,我将为自己生成以下代码:

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,而后者又继承自DbContextAllOtherStuffDbContext (我自己的东西)继承自DbContext

所以我的问题是:

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

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19902756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档