Extending ASP.NET MVC 5 Identity with another obje

2019-04-17 12:15发布

问题:

I'm currently using Fluent API in which I cannot find many resources regarding extending the Identity model with another object.

Here I have my object called ApplicationUser:

public class ApplicationUser : IdentityUser
{
    public virtual Staff Staff { get; set; }

    public int StaffId { get; set; }
}

In which I'd like to map it to my current Staff object:

public class Staff
{
    public int StaffId { get; set; }

    public string DisplayName { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public UserTitleEnum Title { get; set; }

    public string Phone { get; set; }

    public string Email { get; set; }

    public virtual ICollection<Job> Jobs { get; set; }

    public virtual ICollection<LineItem> LineItems { get; set; }

    //Add ApplicationUser here?
}

I have a few limitations as I'm trying to reuse my models by using a PCL profile 78. Because of this, I cannot include EF libraries and have to use Fluent API.

Is there a simpler way to do this or is this the right approach? Any information about how to "extend" or "link" the ApplicationUser object further would be really appreciated.

回答1:

in your DbContext class override OnModelCreating(DbModelBuilder modelBuilder)

modelBuilder.Entity<Staff>().ToTable("Staff");
modelBuilder.Entity<ApplicationUser>().HasRequired(x => x.Staff);

i am sorry for my poor english,hope this helpful