ASP.NET MVC 3 (Adding a New Field to the Movie Model and Table) (8/9)

來源:互聯網
上載者:User
文章目錄
  • 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 to the Movie class. Open the Movie class and add a Rating property (column) that has a StringLength 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 overriding the OnModelCreating method and using the Entity Framework's fluent interface to indicate that "The Price property should 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 for the Movie 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 delete the Movie database. 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 new Rating 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 just delete and re-create it. (You must compile the application in order for scaffolding to see the new Rating field.).

Right-click the Movies/Create.cshtml file and select Delete. In the Movies controller, right-click inside a Create method and select Add View the way you did earlier. Be sure to select Create for Scaffold template. The new Create.cshtml file will include markup for the new Rating field.

For the changes needed in the Movies/Index view, you can edit the file manually. Open the MvcMovie/Views/Movies/Index.cshtml file and add a Rating column heading just after Price. A portion of the changed HTML is shown below:

</th>     <th>         Price     </th>     <th>         Rating     </th> </tr>

Add the Rating property to the foreach loop just after the code for Price. A portion of the changed code is shown below:

Notice that you have IntelliSense to help fill out the item 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.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.