您的位置:首页 > 编程语言 > ASP

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

2017-11-16 15:27 429 查看
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐