2010-12-14 11 views

答えて

9

ため

おかげで一般的に、あなたはそれを行うことはできません - あなたは、ループの中でそれをしなければなりません。しかし、場合によっては、すべてのオブジェクトの追加を避けることができます。具体的には、エンティティグラフがあり、親ノードを追加する場合です。例えば。あなたがEmployeesのコレクションを持ってCompanyオブジェクトがある場合:LINQといくつかのラムダを使用して

context.AddToCompanies(company); 

/* The following loop is not necessary */ 
/* The employees will be saved together with the company */ 
/* 
foreach (var employee in company.Employees) 
{ 
    context.AddToEmployees(employee); 
}*/ 

context.SaveChanges(); 
6

を、あなたはこのように簡単にそれをシードすることができます。

注:現在のバージョンについては、あなたが行うことができますが

List<Company> companies = new List<Company>(); 

companies.ForEach(n => context.AddToCompanies(n)); 

これは、私は、コードファーストアプローチ

List<RelationshipStatus> statuses = new List<RelationshipStatus>() 
{ 
    new RelationshipStatus(){Name = "Single"}, 
    new RelationshipStatus(){Name = "Exclusive Relationship"}, 
    new RelationshipStatus(){Name = "Engaged"}, 
    new RelationshipStatus(){Name = "Married"}, 
    new RelationshipStatus(){Name = "Open Relationship"}, 
    new RelationshipStatus(){Name = "Commited Relationship"} 
}; 

statuses.ForEach(n => myContext.RelationshipStatuses.Add(n)); 
myContext.SaveChanges(); 

コンテキストでエンティティフレームワーク4.1以上で行う方法です。次のようにセットアップされました。

public class MyContext:DbContext 
{ 
    public DbSet<RelationshipStatus> RelationshipStatuses{ get; set; } 
} 
-2

はいあなたは、EntityFramework 6から

List<Employee> empList = this.context.Employee.ToList(); 
関連する問題