Document directory
- Adding a new field to the movie model and table
Adding a new field to the movie model and table
In this section you'll see how easy it is to evolve the schema of our database by simply adding another field toMovie
Class. OpenMovie
Class and addRating
Property (column) that hasStringLength
Attribute applied to it:
[StringLength(5)] public string Rating { get; set; }
While you're adding a new field, let's fix the issue where $9.99 was showing up as $10.00. this version of Entity Framework Code-first has a different default than what we want for decimal values-we want money to have a precision of two decimal points.
Start by overridingOnModelCreating
Method and using the Entity Framework's fluent interface to indicate that "the price property shoshould have a precision of 18 digits to the left of the decimal and 2 digits to the right. "That line is here (In so many words ):
modelBuilder.Entity<Movie>().Property(p => p.Price).HasPrecision(18, 2);
The complete code forMovie
Class is shown below:
using System; using System.Data.Entity; using System.ComponentModel.DataAnnotations; using System.Collections.Generic; using System.Data.Entity.Database; namespace MvcMovie.Models { public class Movie { public int ID { get; set; } [Required] public string Title { get; set; } public DateTime ReleaseDate { get; set; } public string Genre { get; set; } [Required] [Range(5, 100, ErrorMessage = "The price must be between $5 and $100")] public decimal Price { get; set; } [StringLength(5)] public string Rating { get; set; } } public class MovieDBContext : DbContext { public DbSet<Movie> Movies { get; set; } protected override void OnModelCreating ( System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder) { modelBuilder.Entity<Movie>().Property(p => p.Price).HasPrecision(18, 2); } } }
You made another change to the model, so as you did before, use SQL management studio to deleteMovieDatabase. As before, it will be recreated the next time the application runs. (In future tutorials we'll show you how to seed development databases with test data when they're created .)
Compile the application and run it. Your final database is created with the new changes.
We can either add the newRating
Field to the create view manually, or we can delete the create view and regenerate it. since we didn't make any modifications to the scaffolded create view, we'll delete and re-create it. (you must compile the Application in order for scaffolding to see the newRating
Field .).
Right-clickMovies/create. cshtmlFile and selectDelete. InMovies
Controller, right-click insideCreate
Method and selectAdd ViewThe way you did earlier. Be sure to selectCreateForScaffold Template. The newCreate. cshtmlFile will include markup for the newRating
Field.
For the changes needed inMovies/IndexView, You can edit the file manually. OpenMvcmovie/views/movies/index. cshtmlFile and addRatingColumn heading just afterPrice. A portion of the changed HTML is shown below:
</th> <th> Price </th> <th> Rating </th> </tr>
AddRating
Property toforeach
Loop just after the codePrice
. A portion of the changed code is shown below:
Notice that you have intelliisense to help fill outitem
Property. Run the application and the new field is displayed. Enter some movies. Your movie list will look similar to the one below.
In this section we showed how easy it was to update the model. the Code-first approach re-created our database and table using the new schema. now let's add edit, delete, and details views.