1. Create an ASP. Net MVC 3 project --> select razor view engine to provide a default start template for our application.
2. Update or uninstall the re-install extension tool
3. AddProgramPack Entity Framework and efcodefirst
The entityframework Assembly provides significant improvements to. NET data access, including code first. Efcodefirst provides a completely elegant and clean data processing method, so that you no longer need a designer or XML ing file. The ASP. NET mvc3 project can be used easily.
4. implement a data model Product
1 /// <Summary> 2 /// Product entity 3 /// </Summary> 4 Public Class Product 5 { 6 /// <Summary> 7 /// Product ID 8 /// </Summary> 9 Public Int Id { Get ; Set ;} 10 11 /// <Summary> 12 /// Name 13 /// </Summary> 14 Public String Name { Get ; Set ;} 15 16 /// <Summary> 17 /// Category 18 /// </Summary> 19 Public String CATEGORY {Get ; Set ;} 20 21 /// <Summary> 22 /// Unit Price 23 /// </Summary> 24 Public Decimal? Unitprice { Get ;Set ;} 25 26 /// <Summary> 27 /// Inventory 28 /// </Summary> 29 Public Int Unitsinstock { Get ; Set ;} 30 }
5. Implement the context object of the product database in entityframework
/// <Summary>///Context object of the product database in entityframework/// </Summary>Public ClassProductdb: dbcontext {PublicDbset <product> Products {Get;Set;}}
6. Add controllers and views, including the create, edit, details, delete, and index action methods, and view related to each action.
1 Productdb DB = New Productdb (); 2 3 /// <Summary> 4 /// Take the index action method as an example. 5 /// </Summary> 6 /// <Returns> </returns> 7 Public Actionresult index () 8 { 9 // Return view (); 10 VaR Products = From P In DB. Products 11 Where P. unitsinstock> = 0 12 Select P; 13 Return View (products. tolist ()); 14 }
7. Configure the connection string
If the connection string containing the unique identifier name is correctly configured, only the connection string containing the unique identifier name is executed, regardless of whether the automatically generated configuration of the added package Entity Framework and efcodefirst is correct. Otherwise, execute the configuration automatically generated by adding the package Entity Framework and efcodefirst.
1
2
3
"
productdb
" connectionstring =
"
Data Source = .; initial catalog = productdb; Integrated Security = sspi
" providername =
"
system. data. sqlclient
"/>
4
1<! -- Add the automatically generated configurations of the Entity Framework and efcodefirst package -->2<Entityframework>3<Defaconnecticonnectionfactory type ="System. Data. entity. Infrastructure. sqlconnectionfactory, entityframework">4<Parameters>5<Parameter value ="Data Source =.; Integrated Security = true; multipleactiveresultsets = true"/>6</Parameters>7</Defaultonfactory>8</Entityframework>
8. Run the commands respectively --> Add records
9. Differences between the maintenance model and the database structure
EfcodefirstWhen creating a data table, if the field isDecimalType, the default precision is used.(), So that9.99RMB is rounded10RMB. Now we want to change the default precision(18: 2)So that the unit price field in the data table can store two digits after the decimal point.EfcodefirstThis allows you to easily reload the ing rules that define how a model can access data in a database. You can use this overload mechanism to reloadEfcodefirstIn the default type definition and data table inheritance rules, and then access the data.
1 /// <Summary> 2 /// Context object of the product database in entityframework 3 /// </Summary> 4 Public Class Productdb: dbcontext 5 { 6 Public Dbset <product> Products { Get ; Set ;} 7 8 /// <Summary> 9 /// Use the modelbuilder API of EF to define the field precision 10 /// </Summary> 11 /// <Param name = "modelbuilder"> </param> 12 Protected Override Void Onmodelcreating (dbmodelbuilder modelbuilder) 13 { 14 // Base. onmodelcreating (modelbuilder ); 15 Modelbuilder. entity <product> (). Property (P => P. unitprice). hasprecision ( 18 , 2 ); 16 } 17 }
So, start executing --> Error
Why? This problem occurs because the structure of the updated product model class in our application is not consistent with that of the database we actually connect. By defaultEfcodefirstWhen a database is automatically created, efcodefirst automatically adds data tables to the database to synchronize the database structure with the automatically generated model class. If not,EFAn error is thrown. This makes it easier to trace errors during development. Otherwise, you can only find this error at runtime. The synchronization check feature is the cause of the preceding error.
There are two ways to solve this error:
Method 1: Entity Framework automatically deletes the current database and recreates the database based on the new model class. This method is very convenient for development when using a test database, because it allows you to quickly modify the model and database, but will lose the data in the existing database.
Method 2: modify the structure of the data table in the database to match the model. The advantage of this method is that you can retain the data in the table. You can perform this operation manually or create a data table modification script.
1 Public class productintializer: dropcreatedatabaseifmodelchanges
2
{
3
protected
override
void
seed (productdb context)
4
{
5
6
}
7 }
1 protected void application_start () 2 { 3 database. setinitializer
(
New
productintializer ());
4
5
arearegistration. registerallareas ();
6
7
registerglobalfilters (globalfilters. filters);
8
registerroutes (routetable. routes);
9 }
10. Based onCodeMigration
1 Public Partial Class Unitsinstock: dbmigration 2 { 3 Public Override Void Up () 4 { 5 AddColumn ( " Productmodels " , " Unitsinstock " , C => C. INT ()); 6 } 7 8 Public Override Void Down () 9 { 10 Dropcolumn ( " Productmodels " , " Unitsinstock " ); 11 } 12 }