0

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.

Juan José
  • 193
  • 2
  • 3
  • 22
  • Remove `UserId` property from `Employee` and use `Map` to specify the FK column name, e.g. `HasRequired(c => c.User).WithRequiredDependent(u => u.Employee).Map(m => m.MapKey("UserId")).WillCascadeOnDelete(false)` – Ivan Stoev Apr 08 '18 at 13:39
  • the thing is that I need that property, because when I want to save a record in the database with the saveChanges () method it throws me an exception like {"Entities of 'ApplicationDbContext.Employees' are involved in the relation' User_Employee '.' 'User_Employee_Target' was found related to 0. Expected 'User_Employee_Target' of 1."}. – Juan José Apr 08 '18 at 14:00
  • The thing is that as I explained in the linked post, one-to-one with explicit FK property is **not supported** by EF6. You need to revisit your save record procedure (and basically work only with navigation properties). – Ivan Stoev Apr 08 '18 at 14:03
  • So how do you suggest to save a record to the database without the navigation property , because i have that UserId property as the value of a dropdownlist and according to the selected value insert it in the database. – Juan José Apr 08 '18 at 16:28
  • I didn't say to remove the navigation property. Remove the FK property. And *do* use the navigation property. If you have just `UserId`, then create fake (stub) `ApplicationUser` object with just `Id` set, `Attach` it to the context and use it as `User` property of the `Employee`. Anyway, this is completely different question, post another question with the usage scenario and we'll be glad to help. The duplicate is just resolving the model part. – Ivan Stoev Apr 09 '18 at 08:50

0 Answers0