asp.net-mvc - EF6 Scaffolding/w FK to ApplicationUser 无法正确生成

标签 asp.net-mvc entity-framework asp.net-identity

这是一个有点啰嗦的问题。我已经经历了很多尝试同时了解 EF6 和 MVC5 的细节,所以如果我所说的任何内容没有意义或者是明显的错误,我深表歉意。 (如果是的话请告诉我!)

当我尝试为任何具有预制 AspNetUsers 表外键的模型构建 CRUD 页面时,就会出现问题。 (如果您选择具有身份验证的新 MVC5 项目,则此表和其他一些表已经是数据库的一部分。)

我已经能够成功地为我的所有模型构建完全工作的 CRUD 页面,这些页面不包含指向用户表的链接。我想知道这是否是我误解的,或者我所做的事情是否搞砸了。

我在 SA 上阅读了很多与我类似的问题的答案,但无济于事。在我看来,这么简单的事情应该更容易,但我已经为此苦苦挣扎了好几天。

为了尝试隔离问题,我启动了一个带有身份验证的新 MVC 项目,除了添加模型文件、迁移数据库以添加表并尝试搭建页面之外什么也没做。脚手架本身成功完成,但将此行添加到 IdentityModels.cs 文件中:

public System.Data.Entity.DbSet<TestWhatever.Models.ApplicationUser> IdentityUsers { get; set; }

这是不正确的(从另一个 SA 线程了解到)。该文件中应该只有用户生成的数据库集。

运行应用程序后,出现以下错误:

Multiple object sets per type are not supported. The object sets 'IdentityUsers' and 'Users' can both contain instances of type 'TestWhatever.Models.ApplicationUser'.

编辑:下面建议我简单地删除生成的 IdentityUsers 行 - 但是这样做会导致生成的 CRUD 页面中出现编译器错误。这里有些不对劲,我开始认为 EntityFramework 不知道如何使用自己的 UserManager 来显示和更新用户。::

我正在做的事情是正确的吗?我不应该使用内置表进行用户身份验证吗?如果没有,他们为什么在那里?任何见解都值得赞赏,我发现这一切都非常令人困惑,因为我发现的任何文档或回答的问题都从未涵盖完全相同的主题。谢谢。

给我带来问题的表格如下所示:

public class ExamInProgress
{
    [Key]
    public int ProgressId { get; set; }

    [Required]
    [Display(Name = "User")]
    public string UserId { get; set; }
    [ForeignKey("UserId")]
    public ApplicationUser User { get; set; }

    [Required]
    [Display(Name = "Exam")]
    public int ExamId { get; set; }
    public virtual Exam Exam { get; set; }
}


public class CompletedExam
{
    [Key]
    public int CompletedExamId { get; set; }

    [Required]
    [Display(Name = "Date Completed")]
    public DateTime DateCompleted { get; set; }

    [Required]
    [Display(Name = "Final Score")]
    public decimal FinalScore { get; set; }

    [Required]
    [Display(Name = "Exam Name")]
    public string ExamName { get; set; }

    [Required]
    [Display(Name = "User")]
    public string UserId { get; set; }

    [ForeignKey("UserId")]
    public ApplicationUser User { get; set; }
    public virtual Exam Exam { get; set; }
}

我正在使用的表的另一个示例:(还有更多,但主要只是整数和字符串)

public class Exam
{
    [Key]
    public int ExamId { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "Exam name cannot be longer than 100 characters.")]
    [Display(Name = "Exam Name")]
    public string ExamName { get; set; }

    [Display(Name = "Exam Description")]
    public string ExamDescription { get; set; }

    public virtual ICollection<Module> Modules { get; set; }
}

public class Question
{
    [Key]
    public int QuestionId { get; set; }

    [Required]
    [Display(Name = "Question Text")]
    public string QuestionText { get; set; }

    [Display(Name = "Question Order Index")]
    [Range(0, int.MaxValue, ErrorMessage = "Index can not be negative")]
    public int? QuestionOrderIndex { get; set; }

    [Required]
    [Display(Name = "Question Type")]
    public int QuestionTypeId { get; set; }

    [Required]
    [Display(Name = "Module")]
    public int ModuleId { get; set; }

    public virtual QuestionType QuestionType { get; set; }
    public virtual Module Module { get; set; }
    public virtual ICollection<Answer> Answers { get; set; }
}

public class QuestionType
{
    [Key]
    public int QuestionTypeId { get; set; }

    [Required]
    [StringLength(60, ErrorMessage = "QuestionType Name cannot be longer than 60 characters.")]
    [Display(Name = "QuestionType Name")]
    public string QuestionTypeName { get; set; }

    [Display(Name = "QuestionType Description")]
    public string QuestionTypeDescription { get; set; }

    public virtual ICollection<Question> Questions { get; set; }
}

最佳答案

您收到的错误是由于添加了 IdentityUsers属性适合您的上下文。您的上下文继承自 IdentityDbContext<ApplicationUser> ,并且该类已经具有以下属性:

public IDbSet<TUser> Users { get; set; }

哪里TUser是泛型中传递的类型,ApplicationUser 。添加您自己的DbSet对于 ApplicationUser创建两种访问方法,然后您会收到该错误。拿走你的属性(property),你应该就好了。

关于asp.net-mvc - EF6 Scaffolding/w FK to ApplicationUser 无法正确生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24942206/

相关文章:

c# - LINQ to Entities 无法识别方法 'System.Threading.Tasks.Task`

c# - 无法访问已处置的对象 Asp.net Identity Core

asp.net - @RenderBody() 该怎么办?

asp.net-mvc - 在 asp 身份声明和 session 数据之间进行选择

c# - SqlFunctions 和 EntityFunctions 有什么区别?

asp.net - MVC 身份 ||禁用所有 cookie 和 "remember me"选项?

c# - 什么是更好的设计/实践 : Nullable property or 1 value property and 1 bool "has" property?

javascript - Asp.net MVC 在按钮单击时运行 javascript

c# - WPF 有条件地启用键绑定(bind)

c# - Entity Framework : 'x' does not contain a definition for 'y' and no extension method 'y' accepting a first argument of type 'x' could be found