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

asp.net-identity-2 entity-framework-6

Question

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 asp.net 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; }
    }

    [Table("EMPLOYEE")]
    public class Employee
    {
        [Key, Column("ID")]
        public int Id { get; set; }
        [Column("ID_IDIOM")]
        public int IdIdiom { get; set; }
        [Column("USER_LOGIN")]
        [Required]
        [StringLength(50)]
        public string UserName { get; set; }
        [Column("NAME")]
        [Required]
        [StringLength(100)]
        public string Name { get; set; }
        [Column("EMAIL")]
        [Required]
        [StringLength(100)]
        public string Email { get; set; }
        [Column("ACTIVE")]
        public bool Active { get; set; }
        [Column("EMPLOYEE_TYPE")]
        public short EmployeeType { get; set; }
        [Column("SHOW_TUTORIAL")]
        public bool ShowTutorial { get; set; }

        public ApplicationUser User { get; set; }
    }

Any idea will help a lot.

PS.: Sorry for my bad english.

1
3
9/25/2015 5:49:45 PM

Accepted Answer

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

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

See https://msdn.microsoft.com/en-us/data/jj591620.aspx?f=255&MSPPError=-2147217396#RequiredToRequired

3
9/25/2015 6:02:01 PM


Related Questions





Related

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