【问题标题】:DataAnnotation and Code First to Create Foreign Key to ApplicationUser using Identity 2.2.1DataAnnotation 和 Code First 使用 Identity 2.2.1 为 ApplicationUser 创建外键
【发布时间】:2016-03-14 06:06:15
【问题描述】:

简介

我正在尝试构建一个代码优先表,该表将有一个与 ApplicationUser 相关联的第三个表。像往常一样,我一直在搜索 SO 和 Google,以找到解决问题的方法,因为我确信我不是第一个遇到此问题的人,但我尝试过的所有解决方案都不起作用。可能是因为我使用的 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; }
}

问题

问题出在任一版本上,我收到以下错误:

在模型生成过程中检测到一个或多个验证错误:

{MyProjectName}.DataContexts.IdentityUserLogin: : EntityType 'IdentityUserLogin' 没有定义键。定义此 EntityType 的键。

{MyProjectName}.DataContexts.IdentityUserRole: : EntityType 'IdentityUserRole' 没有定义键。定义此 EntityType 的键。

IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' 基于没有定义键的类型'IdentityUserLogin'。

IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' 基于没有定义键的类型'IdentityUserRole'。

问题

如何使用 DataAnnotations 使其像 IdentityUserRoles 一样工作如果可能,不创建第三个类来扩展 ApplicationUser 类?

更新 #1

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

【问题讨论】:

  • 问题出在您未显示的 IdentityUserLogin 和 IdentityUserRole 的定义中。我不知道错误与您显示的代码有什么关系。请编辑您的问题以及受问题影响的代码。如果你说你的脚踝疼,然后你露出手腕,那你就无能为力了。
  • 你使用IdentityDbContext<ApplicationUser>作为DbContext吗?
  • @JotaBe 我将您链接到 IdentityUserRoles 的源代码,您可以通过单击它来访问它。然后,您可以查看 IdentityUserLogins 的源代码。
  • 如果您要链接到位于 IdentityContext 中的 ApplicationUser,那么您需要按照 Stephen 的建议让您的应用程序上下文从 IdentityDbContext 继承,或者您需要将克隆的 ApplicationUser 类添加到您的上下文。
  • @StephenReindl,对于身份对象是的。但是对于这些类不,我创建了另一个上下文来保持身份和我的其他应用程序特定类之间的迁移分离。我应该使用相同的上下文吗?我试试看。

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多