`
suizhikuo
  • 浏览: 28934 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

 
阅读更多

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.

MovieList

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.

分享到:
评论

相关推荐

    ASP.NET MVC MSDN 文档 CHM

    Walkthrough: Adding ASP.NET AJAX Scripting to an MVC Project Action Filtering in MVC Applications How to: Deploy an ASP.NET MVC Application How to: Add a Custom MVC Test Framework in Visual Studio ASP...

    asp.net mvc

    To update the custom controller factories to support ASP.NET MVC 2, change the method signature or signatures to match the new signatures, and use the request context parameter instead of the ...

    Test-Drive ASP.NET MVC

    1 Getting Started with ASP.NET MVC 1.1 How ASP.NET MVC Works 1.2 Installing MVC 1.3 MVC in Five Minutes: Building Quote-O-Matic 2 Test-Driven Development 2.1 TDD Explained 2.2 Test-Driving ...

    ASP.NET MVC 4 Recipes: A Problem-Solution Approach

    •migrating a project from ASP.NET web forms to the MVC 4 including recipes for converting DataGrids, Forms, Web Parts, Master Pages and navigation controls •Client side data binding and templating ...

    ASP.NET.MVC.5.with.Bootstrap.and.Knockout.js.1491914394

    With this practical book, you’ll learn how by combining the ASP.NET MVC server-side language, the Bootstrap front-end framework, and Knockout.js—the JavaScript implementation of the Model-View-...

    ASP.NET Core 1.1 For Beginners: How to Build a MVC Website

    The goal is to get you familiar with ASP.NET Core 1.1 by adding middleware and services one piece at a time, building a basic application. Then you will build a second MVC application using a ...

    Professional ASP.NET 3.5 SP1 Edition: In C# and VB(part1)

    Packed with valuable coverage of ASP.NET 3.5 SP1, this essential resource offers both C# and VB examples throughout the book, and shares new and updated content on the ADO.NET Entity Framework, ADO...

    asp.net core英文版教程

    - **Adding a New Field**:添加新字段的方法。 - **Adding Validation**:添加验证逻辑的技巧。 - **Examining the Details and Delete methods**:分析详情和删除方法的工作流程。 - **ASP.NET Core on Nano ...

    ASP.NET MVC Flash 在线拍照

    // Adding it to the page; var pic = '/Content/' + msg.filename; //$("#photos").attr("src", pic); $("#photos").append(';" src="' + pic + '"/&gt;'); } }); //错误 webcam.set_hook('onError', ...

    ubuntu vps安装docker报错:Cannot connect to the Docker daemon at unix:///var/run/docker.sock.问题解决

    在Ubuntu VPS上安装Docker时,可能会遇到一个常见的错误:“Cannot connect to the Docker daemon at unix:///var/run/docker.sock.” 这个问题通常是由于Docker守护进程未运行或者是由于Linux内核版本过低导致的。...

    ASP.NET Core 实战

    ASP.NET Core 实战 ASP.NET Core 是一个开源的、跨平台的 Web 应用程序框架,由 Microsoft 开发。它提供了一个灵活的、模块化的架构,允许开发者快速构建高性能的 Web 应用程序。本书《ASP.NET Core 实战》旨在帮助...

    Professional.ASP.NET.2.0.AJAX

    &lt;br&gt;What you will learn from this book &lt;br&gt;How to create a better user experience by adding more dynamic UIs Steps for accessing ASP.NET profile and authentication services Ways to ...

    SignalR Programming in Microsoft ASP.NET

    Get definitive guidance on SignalR, a new library for ASP.NET developers that simplifies the process of adding real-time web functionality to your applications. Real-time web functionality enables ...

    Sculpture 2.0 Model-driven development Generators(Installer)

    Sculpture comes with a host of ready-made Molds (The word “Molds” comes from Molding) like (DAAB, NHibernate, LINQ, CSLA, WCF, ASMX, Windows forms, WPF, Silverlight, ASP.NET, & ASP.NET MVC)....

    Adding a Build Banner to ASP.NET Pages

    在ASP.NET开发中,添加构建横幅是一种常见做法,它有助于开发者在页面顶部或底部显示当前应用程序的版本信息、构建日期等,以便于追踪和管理不同的应用版本。这个过程涉及了Web开发的基本概念,包括HTML、CSS、以及...

    Sculpture 1.0 Model-driven development Generators(Installer)

    Sculpture comes with a host of ready-made Molds (The word “Molds” comes from Molding) like (DAAB, NHibernate, LINQ, CSLA, WCF, ASMX, Windows forms, WPF, Silverlight, ASP.NET, & ASP.NET MVC)....

    毕业论文asp.net736(CS)企业员工考勤管理系统sqlserver.doc

    It adopts ASP.NET technology, a Client/Server (C/S) structure, and SQL database for development, ensuring data security and stability, thereby realizing basic operations such as adding, deleting, ...

    Sculpture 1.0 Model-driven development Generators(Source Code)

    Sculpture comes with a host of ready-made Molds (The word “Molds” comes from Molding) like (DAAB, NHibernate, LINQ, CSLA, WCF, ASMX, Windows forms, WPF, Silverlight, ASP.NET, & ASP.NET MVC)....

    Sculpture 1.0 Model-driven development Generators(Molds Source Code)

    Sculpture comes with a host of ready-made Molds (The word “Molds” comes from Molding) like (DAAB, NHibernate, LINQ, CSLA, WCF, ASMX, Windows forms, WPF, Silverlight, ASP.NET, & ASP.NET MVC)....

Global site tag (gtag.js) - Google Analytics