如何在身份用户和我的员工之间创建一对一关系 table
How create one-to-one relationship between identity user and my Employee table
我正在尝试在我的 AspNetUser (ApplicationUser class) table 和我的员工 table.
之间创建一对一的关系
问题是:
- 我的应用程序与我的员工 table 有很多关系,所以我无法创建复合键。
我尝试过的:
- 我尝试使用唯一索引,但entity framework不支持索引。
- 我试图将外键放在 ApplicationUser class 中,但我猜想 asp.net 身份不支持 AspNetUsers 中的复合键 table。
这是我希望实现的示例:
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; }
}
任何想法都会有很大帮助。
PS.: 对不起我的英语不好
如果您的应用程序上下文继承自 IdentityDbContext,则可以使用 fluent api 进行配置:
modelBuilder.Entity<Employee>()
.HasRequired(e => e.User)
.WithRequiredDependent(u => u.Employee);
见https://msdn.microsoft.com/en-us/data/jj591620.aspx?f=255&MSPPError=-2147217396#RequiredToRequired
我正在尝试在我的 AspNetUser (ApplicationUser class) table 和我的员工 table.
之间创建一对一的关系问题是:
- 我的应用程序与我的员工 table 有很多关系,所以我无法创建复合键。
我尝试过的:
- 我尝试使用唯一索引,但entity framework不支持索引。
- 我试图将外键放在 ApplicationUser class 中,但我猜想 asp.net 身份不支持 AspNetUsers 中的复合键 table。
这是我希望实现的示例:
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; }
}
任何想法都会有很大帮助。
PS.: 对不起我的英语不好
如果您的应用程序上下文继承自 IdentityDbContext,则可以使用 fluent api 进行配置:
modelBuilder.Entity<Employee>()
.HasRequired(e => e.User)
.WithRequiredDependent(u => u.Employee);
见https://msdn.microsoft.com/en-us/data/jj591620.aspx?f=255&MSPPError=-2147217396#RequiredToRequired