[英]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.