Home > Software design >  Should you store additional user informations in business database whilst using ASP .Net Identity?
Should you store additional user informations in business database whilst using ASP .Net Identity?

Time:07-02

so - I've just started working with ASP .Net Identity framework in my newbie project but I've stumbled upon a logic problem regarding users that I don't know how to resolve, so let me tell what I mean by that.

In my web project I use two databases (both of them uses different db context):

  • Business database
  • ASP .Net Identity database

Before adding Identity to my project, I've stored all of users informations in my business database (using EntityFramework Core for migrations etc.), their model is like this:

public abstract class User
{     
    public int Id { get; set; }

    [Required(ErrorMessage = "Input login")]
    public string Login { get; set; }

    [Required(ErrorMessage = "Input password")]
    [Display(Name = "Password")]
    public string Pswrd { get; set; }

    [Required(ErrorMessage = "Input mail")]
    [Display(Name = "E-mail")]
    public string Email { get; set; }

    [Display(Name = "Phone number (optional)")]
    [Range(111111111, 999999999)]
    public int Phone_nr { get; set; }
}

And then I have two roles for my users which were selected while registering:

public class Poster : User
{
        [Display(Name = "Questions asked (total)")]
        public int Total_posted { get; set; }


        //1 to N
        public List<Question> Questions { get; set; }
}

public class Answerer : User
{
        [Display(Name = "Answers posted (total)")]
        public int Total_answered { get; set; }


        //1 to N
        public List<Question> Questions { get; set; }
}

Now - when I've added Identity to my project (along with database containing things like users, roles etc.), I figured that I won't need to store informations like id's, passwords, e-mails in my business database but in identity one.

And then comes my problem and my question - should all of user informations be stored in Identity database by customizing IdentityUser class so it will contain some additional properties and just ditch the users table in business db? Because if yes, then how would I include things like foreign keys (or actually - what would be an alternative to them, because I don't know if it will even be possible between two different databases)? Because as it can be seen - my user classes must include references to other class ("Question" class in my case).

CodePudding user response:

Nevermind, I've found needed informations in this answer (even though I searched before, I've just now found that whole post and answer) - using EF Core IdentityContext and DbContext both for order management

So basically - if I want to use seperate databases with entities that relate to themselves, then I have to create kind of dynamic relation based on using some property that will be "shared" among both of entities. Then I can use one of those properties (that will have the same value in both entities) to select those other entities that should be related with it.

If I would like to store all of my data in one database then I would just make my database context inherit from IdentityDbContext. But that wasn't the case here because I wanted to seperate both data sets from eachother.

And concerning modifying Identity users - it's all in doc mentioned by Yiyi You, you just need to create a class that will inherit from "IdentityUser" with some additonal properties that we want in our users, change used user in our database context (so in "AddDbContext") and in said database context class (modify inheritance), then just migrate and update.

  • Related