In this section, you will use Entity Framework Code First to perform model class operations. So that these operations and changes can be applied to the database.
By default, as you did in the previous tutorial, Entity Framework Code First is used to automatically create a database, and Code First is the table added to the database, it will help you track whether the database is synchronized with the model class generated from it. If they are not synchronized, Entity Framework will throw an error. This is very convenient for you to find errors during development, or you may find this problem only during runtime.
Set Code First Migrations for changes to the object model
Double-click Solution ExplorerMovies. mdfOpen the database tool, right-click the database tool (Database Resource Manager, Server resource manager, or SQL Server Object Resource Manager ),Movies. mdfF, and selectDelete.
Build the application to ensure there are no compilation errors.
SlaveToolsClickLibrary Package ManagerAnd then clickPackage Manager Console.
InPackage Manager Console window, at the promptPM> enter:
Enable-Migrations-ContextTypeName MvcMovie. Models. MovieDBContext
As shown aboveEnable-MigrationsCommandMigrationsCreate a folderConfiguration. csFile.
Open in Visual StudioConfiguration. csFile. Replace the Seed function with the following code:
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 } ); }
Right-click Movie under the red wavy line and select "Resolution (Resolve), And then clickUsing MvcMovie. Models;
The following statement is added:
Using MvcMovie. Models;
Code First Migrations calls the Seed method (Package Manager ConsoleUpdate database). This method is used for updates data (if the data exists) or inserted data.
In the AddOrUpdate method, execute an "upsert" Operation in the following code:
context.Movies.AddOrUpdate(i => i.Title, new Movie { Title = "When Harry Met Sally", ReleaseDate = DateTime.Parse("1989-1-11"), Genre = "Romantic Comedy", Rating = "PG", Price = 7.99M }
Because the Seed method runs simultaneously with each migration, you cannot just insert data, because when you are trying to add, you may have completed the first migration after creating the database. The "upsert" operation prevents errors. If you try to insert an existing row, it overwrites any data changes when you test the application. You may not want this to happen: in some cases, when you change the data test, you want the database to be updated synchronously after your change. In this case, you want to insert a row only when it does not exist.
The first parameter passed to the AddOrUpdate method. The specified attribute is used to check whether a row exists. The Title attribute can be used for the data of the test film you provide, because each Title is unique in the list:
Context. Movies. AddOrUpdate (I => I. Title,
This Code assumes that the titiles attribute is unique. If you manually add a duplicate title, you will get the following exception.