MVC5 Identity + EntityFramework 中的多个上下文 [英] MVC5 Identity + multiple context in EntityFramework

查看:16
本文介绍了MVC5 Identity + EntityFramework 中的多个上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MVC 5 应用程序,它使用默认身份验证.用户配置文件是我们模型的一部分,这意味着有几个类具有指向 UserInfo 的外键.有 2 个 DbContext,一个用于模型,另一个用于 UserInfo.

I have a MVC 5 application, which uses the Default Identity authentication. The user profile is part of our model, which means that there are several class which have a foreign key to the UserInfo. There are 2 DbContext, one for the model and another for the UserInfo.

 public class ApplicationDbContext : IdentityDbContext<UserInfo>
    {
        public ApplicationDbContext()
            : base("Profile")
        {
        }

    }



 public class UserInfo : IdentityUser
        {
            public UserInfo ()
            {
                LibraryItems = new List<LibraryItem>();
                if (UserGroup == UserGroupEnum.ANALYST)
                {
                    Companies = new List<Company>();
                }
                DateCreate = DateTime.Now;
                DateUpdate = DateTime.Now;
                DateDelete = DateTime.Now;
            }

            [DisplayColumnInIndex]
            [DisplayName("Is Active ?")]
            public bool IsActive { get; set; }

            [Timestamp]
            public byte[] RowVersion { get; set; }

            //[ValidateFile(ErrorMessage = "Please select a PNG image smaller than 1MB")]
            //[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
            [Editable(false, AllowInitialValue = true)]
            public DateTime DateCreate { get; set; }

            //[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
            public DateTime DateUpdate { get; set; }

            //[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
            public DateTime DateDelete { get; set; }

            [DisplayColumnInIndex]
            public String Name { get; set; }
            [DisplayColumnInIndex]
            public String FirstName { get; set; }

            [DisplayColumnInIndex]
            public String Pseudo { get; set; }

            [DisplayColumnInIndex]
            public string PhoneNumber { get; set; }

            [DisplayColumnInIndex]
            public String Company { get; set; }

            [DisplayName("Name_Id")]
            [ForeignKey("FullName")]
            public int? NameId { get; set; }
            [DisplayColumnInIndex]
            public UserGroupEnum UserGroup { get; set; }

            [DisplayColumnInIndex]
            public UserStatusEnum UserStatus { get; set; }

            public virtual Name FullName { get; set; }
    }


public class Comment
{
       // more properties
        [DisplayName("UserInfoId")]
        [ForeignKey("UserInfo")]
        public virtual String UserInfoId { get; set; }
}

public class Like
{
      // more properties
        [DisplayName("UserInfoId")]
        [ForeignKey("UserInfo")]
        public virtual String UserInfoId { get; set; }
}

我的 userInfo 也有第二个 DBontext 的其他表的外键.

My userInfo has also foreign keys to the the other tables of the second DBontext.

设计身份验证系统的最佳方法是什么?并维护两个上下文之间的关系.

What is the best way to design our authentication system? and maintain the relationship between the two contexts.

提前致谢.

推荐答案

我最近提出的解决方案是对 ASP.NET 身份数据和您的业务实体使用单一上下文:

The solution I came up with recently is to use single context for both ASP.NET identity data and your business entities:

public class DatabaseContext : IdentityDbContext<UserInfo>
{
    public virtual DbSet<Comment> Comments { get; set; } // Your business entities

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

请注意,DatabaseContext 继承自 IdentityDbContext.

这种方法有一些权衡:例如,您的数据访问层应参考 Microsoft.AspNet.Identity.CoreMicrosoft.AspNet.Identity.EntityFramework;但是,如果您使用依赖注入或实体框架迁移,那么在您的项目中拥有单个数据库上下文会使事情变得更加容易.

There are some trade-offs with this approach: for example, your data access layer should reference Microsoft.AspNet.Identity.Core and Microsoft.AspNet.Identity.EntityFramework; however, having a single database context in your project makes things much easier if you are using dependency injection or Entity Framework migrations.

这篇关于MVC5 Identity + EntityFramework 中的多个上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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