question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I perform database initialization/seeding with EF7?

See original GitHub issue

Hi,

I’ve created a code-first model with EF7 beta7 in an ASP.NET 5 MVC 6 project.

Now I want to have some users registered and some other sample data initialized when the application runs, how is this done the proper way?

I used to do this with DbInitializer, but I believe this technique has been retired now. The whole subject of initialization is blurry to me.

When I create a new project and expand the data model, am I supposed to erase the Migrations folder and add a new migration all anew?

Is there a tutorial that explains how migrations, snapshots, and database initialization work in EF7?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

10reactions
rowanmillercommented, Sep 9, 2015

Nothing yet, but we will have content in our docs.

You would put initialization logic in your app startup - Main, Startup.cs, TextFixture, etc. depending on the application type.

If you are using migrations there is context.Database.Migrate(). If you don’t want migrations and just want a quick database (usually for testing) then use context.Database.EnsureCreated()/EnsureDeleted().

Seed data would just go inline with your startup code too, here is an example in an ASP.NET 5 application:

1reaction
jeffmoretti86commented, May 30, 2016

Hi there,

I myself am not Rowan, and I do not have a guide to setup and run MVC/EF7. However, if you want to see some sample code on how to seed with EF7, you may find my blog posting helpful.

http://coderhints.com/ef7-seed-user/

Regards,

Jeff

From: gclaasen Sent: Monday, May 30, 2016 10:10 AM To: aspnet/EntityFramework Cc: jeffmoretti86 ; Comment Subject: Re: [aspnet/EntityFramework] How do I perform database initialization/seeding with EF7? (#3042)

Hi Rowan,

I am starting out with MVC and EF7 and need help. i came across your UnicornStore sample app and would like to know whether you have a guide to how to set it up to run. i tried everything and nothing.

your help will be appreciated.

Thanks (gartycx@gmail.com)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I perform database initialization/seeding with EF7?
I've created a code-first model with EF7 beta7 in an ASP.NET 5 MVC 6 project. Now I want to have some users registered...
Read more >
Data Seeding - EF Core
A straightforward and powerful way to perform data seeding is to use DbContext.SaveChanges() before the main application logic begins execution.
Read more >
3 Ways for Adding Initial Data with Entity Framework Core ...
This video will show you how to add initial data to your database ... Customization 07:00 Using Custom Initialization logic 09:25 Outro ...
Read more >
Migrations and Seed Data With Entity Framework Core
EF Core provides a method called Migrate to execute migration actions for us. All we have to do is to create model classes,...
Read more >
Seed initial data in Entity Framework 7 RC 1 and ASP.NET ...
We can create a method SeedData that extends the IApplicationBuilder then gets an instance of our database context class through GetService ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found