Rifki
Rifki

Reputation: 901

Using context.SaveChanges() in Entity Framework

Hi I'm using the Code first method within Entity Framework using Visual Studio 2012 Could someone tell me if I should add context.SaveChanges(); at the end of my Seed method within the configuration.cs file in order to successfully update my tables? I can make structural changes using update-database -verbose in the console, but this doesn't update my data.

Upvotes: 2

Views: 2031

Answers (2)

NSGaga
NSGaga

Reputation: 14302

Initializers that provide Seed usually look like this inside...

// ...do something - prepare Db, Create etc.
Seed(context); // you override that
context.SaveChanges();

...just to clarify further.

Upvotes: 2

Erik Schierboom
Erik Schierboom

Reputation: 16636

edit: you do in fact not need to call SaveChanges. Thus, this must mean that after the Seed method has been called, a call to SaveChanges is automatically made. However, it is perhaps useful to note that you can call SaveChanges() within the Seed method if you want, which can be useful when dealing with foreign key relations.

Upvotes: 8

Related Questions