简体   繁体   English

序列在实体框架代码优先迁移中包含多个元素错误

[英]Sequence contains more than one element error in Entity framework code-first migrations

I created some models, added the migration and then did an update database operation, though at my last update database operation I got the error message saying: 我创建了一些模型,添加了迁移,然后进行了更新数据库操作,尽管在上次更新数据库操作中,我收到了以下错误消息:

Sequence contains more than one element 序列包含多个元素

Below you can find my migration configuration: 您可以在下面找到我的迁移配置:

context.Restraunts.AddOrUpdate(r => r.name,
            new Restraunt { name = "farzi", city = "a", country = "b" },
             new Restraunt { name = "prime", city = "c", country = "d" },
              new Restraunt { name = "lord", city = "e", country = "f" },
              new Restraunt
              {
                  name = "barracks",
                  city = "g",
                  country = "h",
                  Reviews = new List<RestrauntReview>{
                      new RestrauntReview{rating=5,body="good food!",reviewername="vipul"}
              }


              });

        for (int i = 0; i < 1000; i++)
        {
            context.Restraunts.AddOrUpdate(r => r.name, new Restraunt { name = i.ToString(), city = "nowhere", country = "USA" });
        }
    }
}

Below also you can find my models definitions. 您还可以在下面找到我的模型定义。

public class Restraunt
{
    public int Id { get; set; }
    public string name { get; set; }
    public string city { get; set; }
    public string country { get; set; }
    public virtual ICollection<RestrauntReview> Reviews { get; set; }
}


public class RestrauntReview : IValidatableObject
{
    public int id { get; set; } 

    [Required]
    [Range(1,10)]
    public int rating { get; set; } 

    [Required]
    [StringLength(1024)]
    public string body { get; set; }

    [Display(Name="USER NAME")]
    [DisplayFormat(NullDisplayText="ANONYMOUS")]
    public string reviewername { get; set; }
    public int RestrauntID { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (rating < 2 && reviewername.ToLower().StartsWith("vipul"))
        {
            yield return new ValidationResult("sorry vipul... get out!!");
        }
    }
}

You have to define a Foreign Key as shown below on the RestrauntReview model. 您必须定义一个外键,如下所示在RestrauntReview模型上。

    [ForeignKey("RestrauntID ")]
    public virtual Restraunt Restraunt { get; set; }
    public int RestrauntID { get; set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 序列在EF CF迁移中包含多个元素错误 - Sequence contains more than one element error in EF CF Migrations 实体框架迁移:序列包含多个匹配元素 - Entity Framework Migration: Sequence contains more than one matching element 实体框架 | 序列包含多个匹配元素 - Entity Framework | Sequence contains more than one matching element 实体框架6代码优先的迁移生成 - Entity Framework 6 code-first migrations generation 强制使用实体框架代码优先的ICollection &lt;&gt;中的一个或多个 - Forcing a one-or-more in the ICollection<> with Entity Framework Code-First 检查记录是否存在返回“序列包含多个匹配元素”。 对于带有实体框架 5 的 Net5 - Checking if a record exist returns "Sequence contains more than one matching element." for Net5 with Entity Framework 5 如何正确地促进实体框架代码优先迁移到生产中? - how to properly promote Entity Framework code-first migrations into production? 无法创建实体框架代码优先迁移 - Can't create Entity Framework code-first migrations 在实体框架代码优先迁移中选择列名 - Choosing name of column in entity framework code-first migrations 序列包含一个以上的元素c#实体种子 - Sequence contains more than one element c# Entity Seed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM