EF6脚手架/ w FK到ApplicationUser无法正常生成

时间:2014-07-24 19:20:14

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

这是一个冗长的问题。我曾经经历过很多尝试同时学习EF6和MVC5的来龙去脉,所以如果我说的任何事情没有意义或者说是错误的,我会道歉。 (如果是的话请告诉我!)

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

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

我已经在SA上阅读了很多关于问题的答案,这些问题与我的相似,但无济于事。在我看来,这样一个简单的事情应该更容易,但我已经在这几天里一直在努力。

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

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

哪个不正确(从另一个SA线程中了解到)。此文件中只应有用户生成的dbsets。

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

  

不支持每种类型的多个对象集。该对象设置了IdentityUsers&#39;和&#39;用户&#39;可以包含&#39; TestWhatever.Models.ApplicationUser&#39;类型的实例。

编辑:下面建议我只删除生成的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; }
}

1 个答案:

答案 0 :(得分:3)

您获得的错误是由于将IdentityUsers属性添加到您的上下文中。您的上下文继承自IdentityDbContext<ApplicationUser>,而该类已经具有以下属性:

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

TUser是通用ApplicationUser中传递的类型。为DbSet添加自己的ApplicationUser会创建两种访问方法,并且您会收到该错误。删除你的财产,你应该是好的。