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
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
The complete code for the
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
Right-click the Movies/Create.cshtml file and select Delete. In the
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:
Add the
Notice that you have IntelliSense to help fill out the
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.
In this section you'll see how easy it is to evolve the schema of our database by simply adding another field to the
Movieclass. Open the
Movieclass and add a
Ratingproperty (column) that has a
StringLengthattribute 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
OnModelCreatingmethod 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
Movieclass 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
Ratingfield 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
Ratingfield.).
Right-click the Movies/Create.cshtml file and select Delete. In the
Moviescontroller, right-click inside a
Createmethod 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
Ratingfield.
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
Ratingproperty to the
foreachloop 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
itemproperty. 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.
相关文章推荐
- 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
- ASP.NET MVC 3 (Adding Validation to the Model) (7/9)
- ASP.NET MVC 3 (Adding Validation to the Model) (7/9)
- (转)How to use the ASP.NET MVC ModelBinder
- HTML5, jQuery Mobile and ASP.NET MVC 4 – Using the ViewModel between the model and controller
- The DefaultModelBinder in ASP.NET MVC -ASP.NET MVC的默认模型绑定器
- How to pass javascript complex object to ASP.NET Web Api and MVC
- EF Database First with ASP.NET MVC: Creating the Web Application and Data Models开发遇到问题的解决办法
- Asp.Net Mvc: Model Binding to Simple Types, Co
- ASP.NET MVC- Model- An Introduction to Entity Framework for Absolute Beginners
- asp.net配置报错 “An error was encountered. Please return to the previous page and try again. ”
- 【引】How to Choose the Best Way to Pass Multiple Models in ASP.NET MVC
- 解决ASP.NET MVC(post数据)Json请求太大,无法反序列化(The JSON request was too large to be deserialized)
- ASP.NET MVC - how to make users confirm the delete
- How to achive the CRUD in ASP.NET MVC(VS2010)
- ASP.Net MVC – What are the uses of Display, DisplayName, DisplayFormat and ScaffoldColumn attributes
- Announcing the Release of ASP.NET MVC 5.1, ASP.NET Web API 2.1 and ASP.NET Web Pages 3.1 for VS2012
- [转]Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application (3 of 10)
- [学习心得][Introduction to ASP.NET Core 1.0]3-2 ASP.NET Core and MVC Pattern