How do I perform database initialization/seeding with EF7?
See original GitHub issueHi,
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:
- Created 8 years ago
- Reactions:2
- Comments:9 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 usecontext.Database.EnsureCreated()/EnsureDeleted()
.Seed data would just go inline with your startup code too, here is an example in an ASP.NET 5 application:
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.