I was able to create a one-to-one relationship between the AspNetUsers table and my domain model Employee like this:
public class Employee
{
public int Id { get; set; }
[Required]
[StringLength(50)]
public string Name { get; set; }
public ApplicationUser User { get; set; }
[Required]
[Display(Name = "Employee")]
public string UserId { get; set; }
}
And this is the ApplicationUser class:
public class ApplicationUser : IdentityUser
{
public Employee Employee { get; set; }
public ApplicationUser()
{
Employee = new Employee();
}
}
And this is the configuration of the one-to-one relationship that I use with FluentAPI in a configuration class:
public class EmployeeConfiguration: EntityTypeConfiguration<Employee>
{
public EmployeeConfiguration()
{
HasRequired(c => c.User)
.WithRequiredDependent(u => u.Employee)
.WillCascadeOnDelete(false);
}
}
My issue is that when I run the migration, creating the table generates an extra User_Id column, which would be the foreign key, but what I want is to use the UserId property that I added in my domain model Employee as the foreign key because I need to use that property later.
This is the migration:
public override void Up()
{
CreateTable(
"dbo.Employees",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(nullable: false, maxLength: 50),
UserId = c.String(nullable: false), //I want this
//property as the foreign key and not User_Id
User_Id = c.String(nullable: false, maxLength: 128),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.AspNetUsers", t => t.User_Id)
.Index(t => t.User_Id);
}
Some idea of how I can make the UserId property of the domain model the foreign key and not User_Id. Previously I was able to create the one-to-one relationship with other entities but with ASP.NET Identity entities I can not.