Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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; }
}

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.

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
277 views
Welcome To Ask or Share your Answers For Others

1 Answer

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")
    {
    }
}

Notice that the DatabaseContext inherits from the IdentityDbContext<UserInfo>.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...