1. Select the mvc3 Web Application Project
2. Select an empty template. For the view engine, select razor.
3. After the project is created, we can see that the default JS is not the latest, so we use nuget to update the related component packages in the project.
4. Right-click the project and choose manage nuget packages.
5. nuget will list all the updatable components in the project. We need to update jquery, jquery UI, and Entity Framework to the latest version, and click Install.
6. After the update, we can see that all the relevant files have been updated to the latest version.
7. the environment has been set up. Now a simple system is created. The database has a user table tbl_user, which allows users to log on, modify, and perform other operations. The system uses the new feature of ef-database migration, we do not directly create any data tables. All data tables are automatically generated through EF.
Create a model corresponding to the tbl_user table. we name it tbl_user (when it is automatically created, the class name is the database table name)
We created the following tbl_user model in the project folder models.
Public class tbl_user {[displayname ("ID")] [Key] public guid ID {Get; set;} [displayname ("username")] [stringlength (20, errormessage = "enter user name")] [required] public string name {Get; set;} [displayname ("password")] [stringlength (20, minimumlength = 6, errormessage = "Enter at least six passwords")] [datatype (datatype. password)] [required] Public String PWD {Get; set;} [displayname ("Mailbox")] [datatype (datatype. emailaddress)] P Ublic string email {Get; set;} [displayname ("Age")] [stringlength (1)] public int age {Get; set ;} [displayname ("Creation Time")] [required] public datetime? Datetime_create {Get; set;} // use an empty type. When binding the interface, the text box generated after binding can be blank if no value exists}
You can add attributes to each attribute to restrict attributes. Ideally, you do not need to manually add descriptive text for attributes on the interface.
When using attribute, You need to introduce the following namespace
using System.ComponentModel;using System.ComponentModel.DataAnnotations;
Common attribute
Required: |
It is a required field, that is, the column corresponding to the specified database cannot be null. |
Stringlength: |
Specifies the length range and minimum length of a field. |
Notmapped: |
This field does not generate columns in the database. |
Key: |
Specify the primary key field in the corresponding database. |
Displayname: |
Name of the property |
Datatype: |
Specifies the name of the additional type to be associated with the data field. A ype enumeration is used to specify the data type, and corresponding verification rules and generation methods are created for it. |
We can use EF to automatically generate the related addition, deletion, modification, and query operations and corresponding interfaces of tbl_user, right-click the project folder, and create a controller named usercontroller (the suffix controller in the Controller name cannot be modified, this is the default MVC Convention). Select the template as the EF controller that contains read/write and view, and select the tbl_user we just created for the model class (the model class must be compiled once for display ), data context we use the automatically created mvcdemocontext
After that, vs automatically generates the view folder view/user corresponding to mvcdemocontext, usercontroller, and usercontroller (including query, creation, editing, details, and deletion pages)
Automatically generate a database through database migration. In the Web. config file, create a SQL database connection named mvcdemoservices.
<connectionStrings> <add name="MVCDemoServices" connectionString="Data Source=.;Initial Catalog=MVCDemo;Persist Security Info=True;User ID=sa;Password=123456" providerName="System.Data.SqlClient" /></connectionStrings>
In the mvcdemocontext constructor, specify the database connection
public class MVCDemoContext : DbContext{ public MVCDemoContext() : base("MVCDemoServices") { } public DbSet<Tbl_User> Tbl_User { get; set; }}
Use the PM console for database migration
- Enter enable-migrations to automatically generate the migrations folder and the file configuration. CS
- Enter add-migration createuser to automatically generate the version file in the migrations folder.
- Enter Update-database to automatically create or update a database
- After entering SQL Server Management studio, we found that the database mvcdemo has been created.
Migration command: |
Enable-migrations // enable database migration and create folders |
|
Add-migration addauthor // addauthor is an arbitrary name and is a version mark. |
|
Update-database // update the database |
|
Update-database-targetmigration: "XXX" // you can trace the database to a specified version. XXX is the specified version without. CS, for example, 201206171450182_deletetest. |
|
The database can be updated only after the EF code is compiled. |
Run the following command in the system: Enter http: // localhost: 3556/user. The default homepage of MVC is http: // localhost: 3556/home. The index page is accessed by default. We do not have this directory, therefore, we run http: // localhost: 3556/user to access the index page.
- Then, create a new user and find that the tbl_user model was written incorrectly. The length of the age field is set to 1. We will return to the model to modify it and remove the length of the age field.
[Displayname ("Age")] public int age {Get; set ;}
- After modification, run the task again and save it successfully.
- In the production database, we found that the type of the email field is nvachar (max). The actual email does not need to be so long. You can change it to 100 characters in length.
[Displayname ("Mailbox")] [datatype (datatype. emailaddress)] [stringlength (100)] Public String email {Get; set ;}
- Compile and run again. The system reports an error.
- The reason is that we modified the data field length of the field in the model, but it was not modified in the database. Therefore, we need to migrate the database using EF for database updates.
- Open the PM console, enter add-migration updatetbl_user to generate an Update file.
- Then, enter Update-database to update the database. The operation is successful again.