DataAnnotation 和 Code First 使用 Identity 2.2.1 为 ApplicationUser 创建外键
DataAnnotation and Code First to Create Foreign Key to ApplicationUser using Identity 2.2.1
简介
我正在尝试首先构建一个代码 table,它将有第三个 table 与 ApplicationUser 相关联。我一直在搜索 SO 和 Google,一如既往,以找到解决我的问题的方法,因为我确定我不是第一个 运行 的人,但所有的解决方案我试过都不行。可能是因为我使用的 ASP.NET Identity (v 2.2.1) 的版本需要与我正在寻找的不同的解决方案,或者有些概念我还不熟悉。
本质上,我要构建的正是 IdentityUserRole class is with different meaning. I want to build it using DataAnnotations and not the method they used。虽然我很欣赏其他解决方案和做同样事情的方法,而且我正在做一个真实的项目,但我确实喜欢学习并且至少想学习如何使用 DataAnnotation 来完成它。
话虽如此,好东西来了:
类
Sections
class,相对于 IdentityRole class
public class Sections {
[Key]
[StringLength(128)]
public virtual String Id { get; set; }
[Required]
[Index("SectionsNameIndex", IsUnique = true)]
[MaxLength(256)]
[Display(Name = "Section Name")]
public virtual String Name { get; set; }
}
这里是 UserSections
class,相对于 IdentityUserRole class
版本 1
public class UserSections {
[Key, Column(Order = 1)]
[Index]
[StringLength(128)]
[ForeignKey("User")]
public virtual String UserId { get; set; }
public virtual ApplicationUser User { get; set; }
[Key, Column(Order = 2)]
[Index]
[StringLength(128)]
[ForeignKey("Section")]
public virtual String SectionId { get; set; }
public virtual Sections Section { get; set; }
}
版本 2
public class UserSections {
[Key, Column(Order = 1)]
[Index]
[StringLength(128)]
public virtual String UserId { get; set; }
[ForeignKey("UserId")]
public virtual ApplicationUser User { get; set; }
[Key, Column(Order = 2)]
[Index]
[StringLength(128)]
public virtual String SectionId { get; set; }
[ForeignKey("SectionId")]
public virtual Sections Section { get; set; }
}
问题
问题出在任一版本上我收到以下错误:
One or more validation errors were detected during model generation:
{MyProjectName}.DataContexts.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
{MyProjectName}.DataContexts.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.
问题
如何使用 DataAnnotations 使其像 IdentityUserRoles
一样工作,如果可能的话, 创建第三个 class 来扩展 ApplicationUser
class?
更新 #1
根据给出的答案,这里有更多信息。我确实创建了一个辅助上下文以使其远离身份上下文。当我尝试使用与标识部分相同的上下文时,它起作用了。但是,有没有办法使用 不同的 上下文来做到这一点?
正如 Stephen Reindl 和 Steve Greene 所评论的那样,我的问题的解决方案是使用与 ApplicationUser
.
相同的上下文
简介
我正在尝试首先构建一个代码 table,它将有第三个 table 与 ApplicationUser 相关联。我一直在搜索 SO 和 Google,一如既往,以找到解决我的问题的方法,因为我确定我不是第一个 运行 的人,但所有的解决方案我试过都不行。可能是因为我使用的 ASP.NET Identity (v 2.2.1) 的版本需要与我正在寻找的不同的解决方案,或者有些概念我还不熟悉。
本质上,我要构建的正是 IdentityUserRole class is with different meaning. I want to build it using DataAnnotations and not the method they used。虽然我很欣赏其他解决方案和做同样事情的方法,而且我正在做一个真实的项目,但我确实喜欢学习并且至少想学习如何使用 DataAnnotation 来完成它。
话虽如此,好东西来了:
类
Sections
class,相对于 IdentityRole class
public class Sections {
[Key]
[StringLength(128)]
public virtual String Id { get; set; }
[Required]
[Index("SectionsNameIndex", IsUnique = true)]
[MaxLength(256)]
[Display(Name = "Section Name")]
public virtual String Name { get; set; }
}
这里是 UserSections
class,相对于 IdentityUserRole class
版本 1
public class UserSections {
[Key, Column(Order = 1)]
[Index]
[StringLength(128)]
[ForeignKey("User")]
public virtual String UserId { get; set; }
public virtual ApplicationUser User { get; set; }
[Key, Column(Order = 2)]
[Index]
[StringLength(128)]
[ForeignKey("Section")]
public virtual String SectionId { get; set; }
public virtual Sections Section { get; set; }
}
版本 2
public class UserSections {
[Key, Column(Order = 1)]
[Index]
[StringLength(128)]
public virtual String UserId { get; set; }
[ForeignKey("UserId")]
public virtual ApplicationUser User { get; set; }
[Key, Column(Order = 2)]
[Index]
[StringLength(128)]
public virtual String SectionId { get; set; }
[ForeignKey("SectionId")]
public virtual Sections Section { get; set; }
}
问题
问题出在任一版本上我收到以下错误:
One or more validation errors were detected during model generation:
{MyProjectName}.DataContexts.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
{MyProjectName}.DataContexts.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.
问题
如何使用 DataAnnotations 使其像 IdentityUserRoles
一样工作,如果可能的话, 创建第三个 class 来扩展 ApplicationUser
class?
更新 #1
根据给出的答案,这里有更多信息。我确实创建了一个辅助上下文以使其远离身份上下文。当我尝试使用与标识部分相同的上下文时,它起作用了。但是,有没有办法使用 不同的 上下文来做到这一点?
正如 Stephen Reindl 和 Steve Greene 所评论的那样,我的问题的解决方案是使用与 ApplicationUser
.