我的模特有什么问题?

时间:2016-11-06 06:54:43

标签: entity-framework

我创建了名为Level的模型,现在我尝试使用Seed方法填充数据库。这是一个模型:

public class Level
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid LevelId { get; set; }

    public string Name { get; set; }

    [ForeignKey("Course")]
    public Guid CourseId { get; set; }

    public virtual Course Course { get; set; }
    public virtual List<Stage> Stages { get; set; }
}

这是种子方法

context.Courses.AddOrUpdate(
            c => c.Name,
            new Course() { Name = "C# for beginners", LanguageId = context.Languages.FirstOrDefault(k => k.Name == "C#").LanguageId, UserId = context.Users.FirstOrDefault(u => u.Login == "user1").UserId },
            new Course() { Name = "Advanced C++", LanguageId = context.Languages.FirstOrDefault(k => k.Name == "C++").LanguageId, UserId = context.Users.FirstOrDefault(u => u.Login == "user2").UserId }
           );

        context.Levels.AddOrUpdate(
            l => l.Name,
            new Level() { Name = "Level 1", CourseId = context.Courses.FirstOrDefault(d => d.Name == "C# for beginners").CourseId },
            new Level() { Name = "Level 2", CourseId = context.Courses.FirstOrDefault(d => d.Name == "C# for beginners").CourseId },
            new Level() { Name = "Level 1", CourseId = context.Courses.FirstOrDefault(d => d.Name == "Advanced C++").CourseId }
            );

我和Levels有很多问题。有一次我在数据库中只有2个对象而不是3个,现在我有

  

序列包含多个元素

错误,我不知道为什么,几分钟前我已经更新了数据库,一切正常。它有什么问题?

谢谢

0 个答案:

没有答案