Efcore: How do I perform database initialization/seeding with EF7?

Created on 7 Sep 2015  Â·  9Comments  Â·  Source: dotnet/efcore

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?

Most helpful comment

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:

All 9 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 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:

That's just awesome. Thanks for sharing the links @rowanmiller!

@rowanmiller, @bricelam
I suppose this issue should be reopened or at least discussed by your team.
For now ef7 migrations doesn't allow to modify data in database. The strategy 'ensure that the data is created' on every application startup looks like... well, it looks strange. For example, I want create the list of cities in database. So, I create new migration to create table, but to fill this table I need to change startup class. Okay...

I can use 'Sql' operation from MigrationBuilder, but it is not very useful. I think it will be more useful if MigrationBuilder will support operations like Insert/Update/Delete or even InsertOrUpdate. Take a look at ecm7migrator object model as example. It is not the best, but good to show what I mean.

@resnyanskiy having a proper hook for seeding is already tracked by https://github.com/aspnet/EntityFramework/issues/629

If you want to see an example of how to do it in the meantime in an ASP.NET 5 app, here is a pattern you can use:

For other applications you would follow the same pattern of doing it on app startup... but it's easier since you don't need to worry about DI.

@rowanmiller
The #629 is exactly what I was talking about. Thanks for the link, I'm subscribed for updates. You are doing great job guys, and it is wonderful that you make it 'Open Source'. Happy New Year! :+1: :fireworks: :christmas_tree:

I have posted a solution on my blog for seeding data in EF7. Feel free to check it out and use it if you find it useful. http://coderhints.com/ef7-seed-user/

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 ([email protected])

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 ([email protected])

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

@gclaasen feel free to open a new issue with details of the error you are seeing etc.

Was this page helpful?
0 / 5 - 0 ratings