Skip to content

Instantly share code, notes, and snippets.

@jwill9999
Created April 5, 2017 00:18
Show Gist options
  • Save jwill9999/dbdf687989a9fb8461d170ca8a0ec1c6 to your computer and use it in GitHub Desktop.
Save jwill9999/dbdf687989a9fb8461d170ca8a0ec1c6 to your computer and use it in GitHub Desktop.
Entity framework code first set up asp.net mvc5 Entity Framework
Delete mdf file in app-data
Press CTRL-SHIFT-B to build the project.
In Package Manager console type: Enable-Migrations -ContextTypeName MvcMovie.Models.MovieDBContext
Enter and await confirmation
This creates a Configuration.cs file in Migrations folder
Add Seed data to this file.
Press CTRL-SHIFT-B to build the project.
add-migrations +Name
update-database
**If error delete mdf file and update-database**
**if error continues delete migration folder and contents and start again**
****seed data example****
protected override void Seed(MvcMovie.Models.MovieDBContext context)
{
context.Movies.AddOrUpdate( i => i.Title,
new Movie
{
Title = "When Harry Met Sally",
ReleaseDate = DateTime.Parse("1989-1-11"),
Genre = "Romantic Comedy",
Price = 7.99M
},
new Movie
{
Title = "Ghostbusters ",
ReleaseDate = DateTime.Parse("1984-3-13"),
Genre = "Comedy",
Price = 8.99M
},
new Movie
{
Title = "Ghostbusters 2",
ReleaseDate = DateTime.Parse("1986-2-23"),
Genre = "Comedy",
Price = 9.99M
},
new Movie
{
Title = "Rio Bravo",
ReleaseDate = DateTime.Parse("1959-4-15"),
Genre = "Western",
Price = 3.99M
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment