Document directory
- No connection string
- Explicit connection string Creation
- No connection string
- Explicit connection string Creation
Original article name: EF code first and data scaffolding with the ASP. net mvc 3 tools update
Address: http://weblogs.asp.net/scottgu/archive/2011/05/05/ef-code-first-and-data-scaffolding-with-the-asp-net-mvc-3-tools-update.aspx
We just released ASP. NET mvc3 tool update, installation address: http://asp.net/mvc
In today's blog, I will detail the two cool features of this update:
- Built-in support for ef4.1, including code first
- Visual Studio supports built-in data scaffolding, which allows you to quickly create a data-driven site.
These two features provide powerful support for Web applications.
Target
To help illustrate how to use these two features, we will create a simple data-driven site that can list our products.
The following shows the same category information when creating or editing a new product.
Now we can create the entire application, including the back-end database. It takes only one or two minutes to use ASP. NET mvc3.
Step 1: Create a new project
We start by creating a new ASP. NET mvc3 project and click File> Create project. We use the Internet project template, which provides a default start template for our application.
When you observe the newly created Project in solution manager, you will see ASP. net mvc3 updated tool added to our ASP. net mvc3 project added a new Assembly entityframework.
The entityframework Assembly implements entity framework4.1, and ef4.1 provides significant improvements to. NET data access, including code first. EF code first 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.
We will use EF code first to implement project data access.
Step 2: implement the data model class
Step 1: We will create two classes: product and category. We will create the data model of our application. We will create a standard poco "simple old C # object" in the models file ". The Code is as follows:
Note that the above class is a standard. NET data type and does not need to be derived from any base class or implement any interfaces.
For each individual attribute, each class has an associated attribute. For example, the product class has an attribute named category so that developers can obtain the category of the product, however, the category class has an attribute named products, so that developers can obtain all products of the category. EF code first can automatically manage these associations (using the primary-foreign key relationship ), you can also delay loading data in the background.
Step 3: Use EF code first to implement the storecontext class
Now we have defined two model classes. Next we will implement the dbcontext class. To use EF code first, we need to use this class to map Model objects to tables in the database. Our implementation is as follows:
We use the storecontext class to map the relationship between our product and category and the database. It is derived from dbcontext in EF code first and provides two attributes associated with tables in the database. For our example, the default "convention is better than configuration" method, which means that the products attribute is mapped to the products table in the database and the categories table in the database. After the blog post, I will discuss how to implement custom ing.
You can add this class to any location of the solution. For example, you can place the class in the \ models folder or in an independent class library project, you may need to add the namespace system before the code. data. entity reference. Dbcontext and dbset are defined here.
Step 4: Build a scaffold for the categories Controller
We have created all the contents for getting or saving data from the database. Now, we create an ASP. net MVC controller to create, edit, delete, and update classified data. In the past, you had to manually write a controller to complete these functions, you can access the data code through EF code first by yourself. Now, Asp. net mvc3 tool updates now include built-in Scaffolding Support to help you automate these tasks.
Set up a new categories controller class. Right-click the/controllers folder and choose add> controller context menu.
Then, the Add controller dialog box is displayed. we name the created controller as categoriescontroller. The current dialog box supports a new scaffold section, allowing us to build a controller using a built-in template.
Choose controller with read/write action and views and using Entity Framework template. This will enable the created controller to include the complete EF add, delete, modify, and query code.
After selecting this template, we select the model class used by the Controller to implement crud support. Here is the dategory class. We also select the EF dbcontext/objectcontext class to access the database. Here, use the storecontext class we defined earlier.
When you click the Add button, Visual Studio automatically creates a categoriescontroller class, including the create, edit, details, delete, and index action methods, there are also view templates related to each action.
If you enable categoriescontroller. CS, you will find that data access via EF is supported in the Action method.
We have implemented all the code to access the categories data.
Step 5: configure the database location
The database location we used in the last step before running. EF code first allows you to use an existing database or a database that does not exist yet. You can use the model class to automatically create it.
No connection string
By default, you do not even need to configure a connection string to specify the database. If not specified, EF code first will create a database with the same name as your dbcontext in SQL Express. \ sqlexpress creates a demo named scaffoldingdemo. models. the database of storecontes.
Explicit connection string Creation
You can also. add a connection string configuration in config to explicitly specify. The connection string name must match the name of the dbcontext class. For example, in our application, the dbcontext name is storecontext, by default, EF searches for connection strings of the same name to determine the database location.
The following is an embedded SQL ce Database Configuration saved in the app_data folder named store. SDF.
<Configuration>
<Connectionstrings>
<Add name = "storecontext"
Connectionstring = "Data Source = | datadirectory | \ store. SDF"
Providername = "system. Data. sqlserverce.4.0"/>
</Connectionstrings>
</Configuration> Step 6: run the application
Okay! Now we run this application and navigate to/categories. This will execute the index method of categoriescontroller to list all categories in the database. Currently, no category exists.
Click the create new link to create a new category. This will navigate to/categories/create. This action provides a form for creating a new category.
Click Create to post to categoriescontroller and add a new category to the database. Repeat this operation to create multiple categories.
The cool thing is that we don't need to write any controllers or views!
Scaffolding automatically generates the required code, which provides a simple starting point. We can continue to adjust it to customize more features.
Step 7: Follow the database
Now, the configuration program uses the Embedded Database SQL Ce. When the program is running, if the database does not exist, EF 4.1 will detect this and then automatically create it, based on the product and category classes we defined earlier.
To view the database in solution, click Refresh in solution manager. Then, store. SDF automatically created by EF will appear in the app_data folder.
You can double-click the database, open it in the server resource manager, and expand tables to view the created table.
Note that the table created by EF is based on the product and category classes, and the association between the primary key and the two tables is automatically created based on the attribute definitions in the class.
Step 8: Build a scaffold for the product Controller
We have created a categoriescontroller to manage categories. Next we will create a productscontroller to manage products.
Right-click the \ controller folder and choose add-> controller to create productscontroller. We use the EF template and product model class.
After clicking the Add button, productscontroller has been set up, including the crud method and the corresponding view.
Run the command and navigate to/products.
Click create new to create a product.
What's cool is that classification is a drop-down list rather than a text box that fills in Foreign keys.
The scaffolding supported in Visual Studio intelligently detects the associated category, and EF code automatically associates with the correct category.
After creating some products, access/products to display the following results.
Note that the classification information is displayed with a friendly name instead of a foreign key value.
Original article name: EF code first and data scaffolding with the ASP. net mvc 3 tools update
Address: http://weblogs.asp.net/scottgu/archive/2011/05/05/ef-code-first-and-data-scaffolding-with-the-asp-net-mvc-3-tools-update.aspx
We just released ASP. NET mvc3 tool update, installation address: http://asp.net/mvc
In today's blog, I will detail the two cool features of this update:
- Built-in support for ef4.1, including code first
- Visual Studio supports built-in data scaffolding, which allows you to quickly create a data-driven site.
These two features provide powerful support for Web applications.
Target
To help illustrate how to use these two features, we will create a simple data-driven site that can list our products.
The following shows the same category information when creating or editing a new product.
Now we can create the entire application, including the back-end database. It takes only one or two minutes to use ASP. NET mvc3.
Step 1: Create a new project
We start by creating a new ASP. NET mvc3 project and click File> Create project. We use the Internet project template, which provides a default start template for our application.
When you observe the newly created Project in solution manager, you will see ASP. net mvc3 updated tool added to our ASP. net mvc3 project added a new Assembly entityframework.
The entityframework Assembly implements entity framework4.1, and ef4.1 provides significant improvements to. NET data access, including code first. EF code first 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.
We will use EF code first to implement project data access.
Step 2: implement the data model class
Step 1: We will create two classes: product and category. We will create the data model of our application. We will create a standard poco "simple old C # object" in the models file ". The Code is as follows:
Note that the above class is a standard. NET data type and does not need to be derived from any base class or implement any interfaces.
For each individual attribute, each class has an associated attribute. For example, the product class has an attribute named category so that developers can obtain the category of the product, however, the category class has an attribute named products, so that developers can obtain all products of the category. EF code first can automatically manage these associations (using the primary-foreign key relationship ), you can also delay loading data in the background.
Step 3: Use EF code first to implement the storecontext class
Now we have defined two model classes. Next we will implement the dbcontext class. To use EF code first, we need to use this class to map Model objects to tables in the database. Our implementation is as follows:
We use the storecontext class to map the relationship between our product and category and the database. It is derived from dbcontext in EF code first and provides two attributes associated with tables in the database. For our example, the default "convention is better than configuration" method, which means that the products attribute is mapped to the products table in the database and the categories table in the database. After the blog post, I will discuss how to implement custom ing.
You can add this class to any location of the solution. For example, you can place the class in the \ models folder or in an independent class library project, you may need to add the namespace system before the code. data. entity reference. Dbcontext and dbset are defined here.
Step 4: Build a scaffold for the categories Controller
We have created all the contents for getting or saving data from the database. Now, we create an ASP. net MVC controller to create, edit, delete, and update classified data. In the past, you had to manually write a controller to complete these functions, you can access the data code through EF code first by yourself. Now, Asp. net mvc3 tool updates now include built-in Scaffolding Support to help you automate these tasks.
Set up a new categories controller class. Right-click the/controllers folder and choose add> controller context menu.
Then, the Add controller dialog box is displayed. we name the created controller as categoriescontroller. The current dialog box supports a new scaffold section, allowing us to build a controller using a built-in template.
Choose controller with read/write action and views and using Entity Framework template. This will enable the created controller to include the complete EF add, delete, modify, and query code.
After selecting this template, we select the model class used by the Controller to implement crud support. Here is the dategory class. We also select the EF dbcontext/objectcontext class to access the database. Here, use the storecontext class we defined earlier.
When you click the Add button, Visual Studio automatically creates a categoriescontroller class, including the create, edit, details, delete, and index action methods, there are also view templates related to each action.
If you enable categoriescontroller. CS, you will find that data access via EF is supported in the Action method.
We have implemented all the code to access the categories data.
Step 5: configure the database location
The database location we used in the last step before running. EF code first allows you to use an existing database or a database that does not exist yet. You can use the model class to automatically create it.
No connection string
By default, you do not even need to configure a connection string to specify the database. If not specified, EF code first will create a database with the same name as your dbcontext in SQL Express. \ sqlexpress creates a demo named scaffoldingdemo. models. the database of storecontes.
Explicit connection string Creation
You can also. add a connection string configuration in config to explicitly specify. The connection string name must match the name of the dbcontext class. For example, in our application, the dbcontext name is storecontext, by default, EF searches for connection strings of the same name to determine the database location.
The following is an embedded SQL ce Database Configuration saved in the app_data folder named store. SDF.
<Configuration>
<Connectionstrings>
<Add name = "storecontext"
Connectionstring = "Data Source = | datadirectory | \ store. SDF"
Providername = "system. Data. sqlserverce.4.0"/>
</Connectionstrings>
</Configuration> Step 6: run the application
Okay! Now we run this application and navigate to/categories. This will execute the index method of categoriescontroller to list all categories in the database. Currently, no category exists.
Click the create new link to create a new category. This will navigate to/categories/create. This action provides a form for creating a new category.
Click Create to post to categoriescontroller and add a new category to the database. Repeat this operation to create multiple categories.
The cool thing is that we don't need to write any controllers or views!
Scaffolding automatically generates the required code, which provides a simple starting point. We can continue to adjust it to customize more features.
Step 7: Follow the database
Now, the configuration program uses the Embedded Database SQL Ce. When the program is running, if the database does not exist, EF 4.1 will detect this and then automatically create it, based on the product and category classes we defined earlier.
To view the database in solution, click Refresh in solution manager. Then, store. SDF automatically created by EF will appear in the app_data folder.
You can double-click the database, open it in the server resource manager, and expand tables to view the created table.
Note that the table created by EF is based on the product and category classes, and the association between the primary key and the two tables is automatically created based on the attribute definitions in the class.
Step 8: Build a scaffold for the product Controller
We have created a categoriescontroller to manage categories. Next we will create a productscontroller to manage products.
Right-click the \ controller folder and choose add-> controller to create productscontroller. We use the EF template and product model class.
After clicking the Add button, productscontroller has been set up, including the crud method and the corresponding view.
Run the command and navigate to/products.
Click create new to create a product.
What's cool is that classification is a drop-down list rather than a text box that fills in Foreign keys.
The scaffolding supported in Visual Studio intelligently detects the associated category, and EF code automatically associates with the correct category.
After creating some products, access/products to display the following results.
Note that the classification information is displayed with a friendly name instead of a foreign key value.