c# - DataAnnotation 和 Code First 使用 Identity 2.2.1 为 ApplicationUser 创建外键

标签 c# entity-framework ef-code-first data-annotations entity-framework-migrations

介绍

我正在尝试构建一个代码优先表,该表将有一个与 ApplicationUser 相关的第三个表。我一直在搜索 SO 和谷歌,一如既往,以找到解决我的问题的方法,因为我确定我不是第一个遇到这个问题的人,但我尝试过的所有解决方案都不起作用。可能是因为我使用的 ASP.NET Identity (v 2.2.1) 版本需要与我正在寻找的解决方案不同的解决方案,或者有些概念我还不熟悉。

本质上,我要构建的正是 IdentityUserRole类具有不同的含义。我想使用 DataAnnotations 而不是 the method they used 来构建它.虽然我很欣赏其他解决方案和做同样事情的方法,而且我正在处理一个真实的项目,但我确实喜欢学习,至少想学习如何使用 DataAnnotation 来完成它。

话虽如此,这是好东西:

Sections 类,相对于 IdentityRole

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 类,它与 IdentityUserRole 相关类

版本 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 一样工作 如果可能的话, 创建第三个类来扩展 ApplicationUser类(class)?

更新 #1

根据给出的答案,这里有更多信息。我确实创建了一个辅助上下文以使其远离身份上下文。当我尝试使用与标识部分相同的上下文时,它起作用了。但是,有没有办法使用不同的上下文来做到这一点?

最佳答案

正如 Stephen Reindl 和 Steve Greene 所说,我的问题的解决方案是使用与 ApplicationUser 相同的上下文。

关于c# - DataAnnotation 和 Code First 使用 Identity 2.2.1 为 ApplicationUser 创建外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34170817/

相关文章:

c# - 检查文件访问权限,获取进程 ID

asp.net-mvc - 在 ASP.NET MVC 中更新多个模型

c# - Entity Framework 中的 ICollection<T> 与 List<T>

entity-framework - Entity Framework v6 GroupBy 丢失原始顺序

c# - 首先通过代码更新具有一对一关系的 ViewModel

.net - Entity Framework 迁移错误-序列不包含任何元素

C# 反射调用新进程中的方法

javascript - 更好的 JavaScript/Typescript 调试器可视化工具

c# - 参数 '@id' ,向数据库插入数据时未提供该参数

c# - 由于关键字为 "using"的未知命名空间,运行转换失败