我首先使用EF代码来构建我的项目。 我使用种子方法初始化我的数据库中的数据,但只有一些类模型数据已在数据库中注册。 我没有任何警告或问题。 举个例子: 1)不
var categories = new List<Categorie>{
new Categorie{Nom="Informatique"},
new Categorie{Nom="Bien Etre"},
};
categories.ForEach(s => context.Categories.Add(s));
context.SaveChanges();
班级模特:
public class Categorie
{
public int CategorieID { get; set; }
public string Nom { get; set; }
public virtual ICollection<Cour> Cours { get; set; }
}
2)完成
var coachs = new List<Coach>
{
new Coach{Nom="Guith",Prenom="Etienne", Login="etienneguith",Password="etienneguith",Profession="Neurologue",Cour="Medecine generale",Email="guithetienne@mic.be",agenda=temp1},
new Coach{Nom="John",Prenom="Robert", Login="robertjohn",Password="robertjohn",Profession="Juge",Cour="Droit Penal",Email="robertjohn@mic.be",agenda=temp2},
};
categories.ForEach(s => context.Categories.Add(s));
context.SaveChanges();
班级模型
public class Coach
{
public int CoachID { get; set; }
public string Nom { get; set; }
public string Prenom { get; set; }
public string Login { get; set; }
public string Password { get; set; }
public string Profession { get; set; }
public string Cour { get; set; }
public string Email { get; set; }
public DateTime agenda { get; set; }
public virtual ICollection<Session> Sessions { get; set; }
}
答案 0 :(得分:3)
看起来您无意中两次迭代类别集合。
var coachs = new List<Coach>
{
new Coach{Nom="Guith",Prenom="Etienne", Login="etienneguith",Password="etienneguith",Profession="Neurologue",Cour="Medecine generale",Email="guithetienne@mic.be",agenda=temp1},
new Coach{Nom="John",Prenom="Robert", Login="robertjohn",Password="robertjohn",Profession="Juge",Cour="Droit Penal",Email="robertjohn@mic.be",agenda=temp2},
};
categories.ForEach(s => context.Categories.Add(s));
context.SaveChanges();
看起来应该是:
var coachs = new List<Coach>
{
new Coach{Nom="Guith",Prenom="Etienne", Login="etienneguith",Password="etienneguith",Profession="Neurologue",Cour="Medecine generale",Email="guithetienne@mic.be",agenda=temp1},
new Coach{Nom="John",Prenom="Robert", Login="robertjohn",Password="robertjohn",Profession="Juge",Cour="Droit Penal",Email="robertjohn@mic.be",agenda=temp2},
};
coachs.ForEach(s => context.Coachs.Add(s));
context.SaveChanges();