How create one-to-one relationship between identity user and my Employee table entity-framework-6


I'm trying to create an one-to-one relationship between my AspNetUser (ApplicationUser class) table and my Employee table.

The problem is:

  • My application has so many relationships with my Employee table, so I can't create a composite key.

What I've tried:

  • I tried to use unique index, but entity framework doesn't Support indexes.
  • I tried to put the foreign key in ApplicationUser class, but I guess identity doesn't support composite key in AspNetUsers table.

Here is a example of what I expect to implement:

public class ApplicationUser : IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim>
        public ApplicationUser()
            : base()
            PreviousUserPasswords = new List<PreviousPassword>();
        public virtual IList<PreviousPassword> PreviousUserPasswords { get; set; }

        public virtual Employee Employee { get; set; }

    public class Employee
        [Key, Column("ID")]
        public int Id { get; set; }
        public int IdIdiom { get; set; }
        public string UserName { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public bool Active { get; set; }
        public short EmployeeType { get; set; }
        public bool ShowTutorial { get; set; }

        public ApplicationUser User { get; set; }

Any idea will help a lot.

PS.: Sorry for my bad english.

9/25/2015 5:49:45 PM

Accepted Answer

This can be configured with the fluent api if your application context inherits from IdentityDbContext:

   .HasRequired(e => e.User)
   .WithRequiredDependent(u => u.Employee);


9/25/2015 6:02:01 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow