Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I perform database initialization/seeding with EF7? #3042

Closed
weitzhandler opened this issue Sep 7, 2015 · 9 comments
Closed

How do I perform database initialization/seeding with EF7? #3042

weitzhandler opened this issue Sep 7, 2015 · 9 comments

Comments

@weitzhandler
Copy link
Contributor

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?

@rowanmiller
Copy link
Contributor

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:

@weitzhandler
Copy link
Contributor Author

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

@resnyanskiy
Copy link

@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.

@rowanmiller
Copy link
Contributor

@resnyanskiy having a proper hook for seeding is already tracked by #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.

@resnyanskiy
Copy link

@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! 👍 🎆 🎄

@jeffmoretti86
Copy link

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/

@gclaasen
Copy link

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)

@jeffmoretti86
Copy link

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.

@rowanmiller
Copy link
Contributor

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

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants