MVC5身份在+多的EntityFramework背景 [英] MVC5 Identity + multiple context in EntityFramework

查看:162
本文介绍了MVC5身份在+多的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; }
}

我的用户信息也有外键到第二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&LT继承;的UserInfo方式&gt;

有一些权衡这种方法:例如,你的数据访问层应该引用 Microsoft.AspNet.Identity.Core 微软.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身份在+多的EntityFramework背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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