gruber
gruber

Reputation: 29729

adding List of objects to Context in ef

Is it possible to add list of object to Context in entity framework without using foreach addObject ?

thanks for help

Upvotes: 39

Views: 75771

Answers (5)

Imran Faruqi
Imran Faruqi

Reputation: 743

You can do it via navigation property. I came across the same problem and I did not feel good using loops.

If you have a Student table and a Subject Table. You will have one to many relationship between Table Student and Table Subject respectively. A navigation property in Student table will be public IEnumerable<Subject> Subjects {get; set;} and a navigation property in Subject table will be public Student Student {get;set;}.

If you want to use loop, you will use the navigation property Student, and if you want to add all the instances of Subjects in Subjects table with corresponding Student, you will use the navigation property Subjects.

How to Use? Suppose you have objects ready to map, I mean you have added the fields in Student and in Subjects.

Now student.Subjects = subjects' then add student using yourDbContext` OR As explained in @Yakimych answer above (commented though).

I have not implemented this in EF but in EF Core 3.1. However, most of the future readers will benefit from this answer including the ones using EF and not EF Core.

Upvotes: 0

Max Alexander
Max Alexander

Reputation: 5581

Using linq and some lambdas you can seed it easily like this.

Note: Regarding your current version you can do

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

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

This is the way I do with Entity Framework 4.1 or higher with the Code First Approach

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

The Context was Setup as follows

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

Upvotes: 11

codeandcloud
codeandcloud

Reputation: 55210

From EntityFramework 6 you can use DbSet.AddRange Method (IEnumerable) like this

db.companies.AddRange(newCompanies);

Upvotes: 66

AKS
AKS

Reputation: 1279

Yes you can, like

List<Employee> empList = this.context.Employee.ToList();

Upvotes: -6

Yakimych
Yakimych

Reputation: 17752

Generally you can't do that - you have to do it in a loop. In some cases, however, you can avoid adding every object - specifically, if you have an entity graph and you add the parent node. E.g. if you have a Company object that has a collection of Employees:

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

Upvotes: 22

Related Questions