2

I am trying to use separate identity classes that inherit from ApplicationUser, and create a relationship between them. Business and Customer where a Business can have many Customers. I get an error when adding relationships. What is the proper way to achieve adding identity between classes with relationships?

ApplicationUser.cs

public class ApplicationUser : IdentityUser
{
    public virtual Business Business { get; set; }
    public virtual Customer Customer { get; set; }
    [Required]
    [Display(Name = "First Name")]
    public string FirstName { get; set; }
    [Required]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }
}

Business.cs

public class Business : ApplicationUser
{
    public string BusinessUserId { get; set; }
    [Required]
    public string BusinessName { get; set; }
    [Required]
    [Display(Name = "Address")]
    public string Address { get; set; }

    public ICollection<Customer> Customers { get; set; }

    ....
}

Customer.cs

    public class Customer : ApplicationUser
    {
       public string CustomerUserId { get; set; }
       public override Business Business { get; set; }
    }
Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
robert_rg
  • 275
  • 1
  • 5
  • 13
  • 1
    You are mixing inheritance with association – Hameed Syed Jan 02 '20 at 04:31
  • I trying to create a login profile for Business and Customer so each can log in as well as create the relationship between the two. – robert_rg Jan 02 '20 at 05:11
  • 1
    By default, EF will implement inheritance via STI (single-table inheritance). Essentially, all properties on ApplicationUser and all derivations will all go into the table for ApplicationUser (i.e. AspNetUsers).If you want to separate to three tables, you may need to use [TPT](https://stackoverflow.com/questions/47733184/table-per-type-in-entity-framework-core-2-0) – Ryan Jan 02 '20 at 05:41
  • Does this link help you?https://stackoverflow.com/questions/27059017/creating-inheritance-users-from-base-asp-net-identity-user – Ryan Jan 02 '20 at 06:55

1 Answers1

3

You are using TPH which will go into the same table(AspNetUser) for your above entities.

To use TPT, you could configure models like

public class ApplicationUser : IdentityUser
{

    public string BusinessId { get; set; }
    [ForeignKey("BusinessId")]
    public virtual Business Business { get; set; }

    public string CustomerId { get; set; }
    [ForeignKey("CustomerId")]
    public virtual Customer Customer { get; set; }

    [Required]
    [Display(Name = "First Name")]
    public string FirstName { get; set; }
    [Required]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }
}

public class Business
{
    [Key]
    public string BusinessUserId { get; set; }
    [Required]
    public string BusinessName { get; set; }
    [Required]
    [Display(Name = "Address")]
    public string Address { get; set; }

    public virtual ApplicationUser User { get; set; }
    public ICollection<Customer> Customers { get; set; }

}

public class Customer 
{
    [Key]
    public string CustomerUserId { get; set; }

    public string BusinessId { get; set; }
    [ForeignKey("BusinessId")]
    public  Business Business { get; set; }

    public virtual ApplicationUser User { get; set; }
}

DbContext:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {

    }


    public DbSet<Customer> Customers { get; set; }
    public DbSet<Business> Businesses { get; set; }

}
Ryan
  • 19,118
  • 10
  • 37
  • 53