ASP. net mvc 3 (adding a new field to the movie model and table) (8/9)

Source: Internet
Author: User
Tags management studio sql management studio
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 toMovieClass. OpenMovieClass and addRatingProperty (column) that hasStringLengthAttribute 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 overridingOnModelCreatingMethod 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 forMovieClass 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 newRatingField 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 newRatingField .).

Right-clickMovies/create. cshtmlFile and selectDelete. InMoviesController, right-click insideCreateMethod and selectAdd ViewThe way you did earlier. Be sure to selectCreateForScaffold Template. The newCreate. cshtmlFile will include markup for the newRatingField.

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>

AddRatingProperty toforeachLoop just after the codePrice. A portion of the changed code is shown below:

Notice that you have intelliisense to help fill outitemProperty. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.