2016-09-28 104 views
2

我創造了一些車型,增加了遷移,然後做了一個更新數據庫的操作,但在我的最後一次更新數據庫的操作,我得到了錯誤信息說:序列包含實體框架代碼優先遷移多個元素錯誤

序列包含多個元素

下面你可以找到我的遷移配置:

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" }); 
     } 
    } 
} 

以下還可以找到我的模型定義。

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!!"); 
     } 
    } 
} 

回答

0

您必須在RestrauntReview模型中定義一個外鍵,如下所示。

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