c# - EF 4.1 代码首先初始化我的数据库

标签 c# database entity-framework-4

我首先使用 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; }

    }

最佳答案

看起来您无意中将 categories 集合迭代了两次。

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(); 

关于c# - EF 4.1 代码首先初始化我的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9778641/

相关文章:

entity-framework - 在 EF 4.1 中使用 DbContext 和数据库优先

c# - 如何从 NpgsqlDataReader 获取行数

entity-framework - Entity Framework - 如果 SaveChanges 失败并且我不想进行某些更改该怎么办?

c# - 使用EF4仅获得SQL表的最后一行的最有效方法是什么?

java - 使用 MySQL 通过 java 更新表内的单个值

python - mysql 连接器不显示插入的结果

mysql - 使用 UPDATE 用 AVG 数据填充第二个表的列

c# - MS 图表 : How can you change the color of each label on the Axis of a Bar Chart?

c# - 使用 C# TCP channel 的双向通信

c# - 构建女妖:缺少依赖项