Create the Data Access Layer
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Create the Data Access Layer,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4641字,纯文字阅读大概需要7分钟。
内容图文
![Create the Data Access Layer](/upload/InfoBanner/zyjiaocheng/486/1275ae6945b74631be37fe0a5ecc6365.jpg)
In this tutorial, you‘ll start out by adding simple entity classes representing the schemas for products and categories.
The products class will contain definitions for each product.
The name of each of the members of the product class will be ProductID
, ProductName
, Description
, ImagePath
, UnitPrice
, CategoryID
, and Category
.
The category class will contain definitions for each category that a product can belong to, such as Car, Boat, or Plane.
The name of each of the members of the category class will be CategoryID
, CategoryName
, Description
, and Products
.
Each product will belong to one of the categories.
These entity classes will be added to the project‘s existing Models folder.
1.In Solution Explorer, right-click the Models folder and then select Add -> New Item.
2.Under Visual C# from the Installed pane on the left, select Code.
3.Select Class from the middle pane and name this new class Product.cs.
4.Click Add.
The new class file is displayed in the editor.
5.Replace the default code with the following code:
6.Create another class by repeating steps 1 through 4, however,
name the new class Category.cs and replace the default code with the following code:
As previously mentioned, the Category
class represents the type of product that the application is designed to sell (such as "Cars", "Boats", "Rockets", and so on),
and the Product
class represents the individual products (toys) in the database.
Each instance of a Product
object will correspond to a row within a relational database table, and each property of the Product class will map to a column in the relational database table.
Later in this tutorial, you‘ll review the product data contained in the database.
Data Annotations
You may have noticed that certain members of the classes have attributes specifying details about the member, such as [ScaffoldColumn(false)]
.
These are data annotations.
The data annotation attributes can describe how to validate user input for that member, to specify formatting for it, and to specify how it is modeled when the database is created.
Context Class
To start using the classes for data access, you must define a context class.
As mentioned previously, the context class manages the entity classes (such as the Product
class and the Category
class) and provides data access to the database.+
This procedure adds a new C# context class to the Models folder.
1.Right-click the Models folder and then select Add -> New Item.
The Add New Item dialog box is displayed.
2.Select Class from the middle pane, name it ProductContext.cs and click Add.
3.Replace the default code contained in the class with the following code:
This code adds the System.Data.Entity
namespace so that you have access to all the core functionality of Entity Framework, which includes the capability to query, insert, update, and delete data by working with strongly typed objects.
The ProductContext
class represents Entity Framework product database context, which handles fetching, storing, and updating Product
class instances in the database.
The ProductContext
class derives from the DbContext
base class provided by Entity Framework.
Initializer Class
You will need to run some custom logic to initialize the database the first time the context is used.
This will allow seed data to be added to the database so that you can immediately display products and categories.
This procedure adds a new C# initializer class to the Models folder.+
1.Create another Class
in the Models folder and name it ProductDatabaseInitializer.cs.
2.Replace the default code contained in the class with the following code:
As you can see from the above code, when the database is created and initialized, the Seed
property is overridden and set.
When the Seed
property is set, the values from the categories and products are used to populate the database.
If you attempt to update the seed data by modifying the above code after the database has been created, you won‘t see any updates when you run the Web application.
The reason is the above code uses an implementation of the DropCreateDatabaseIfModelChanges
class to recognize if the model (schema) has changed before resetting the seed data.
If no changes are made to the Category
and Product
entity classes, the database will not be reinitialized with the seed data.
Create the Data Access Layer
标签:sdn method included asp.net btn att structure lib val
本文系统来源:http://www.cnblogs.com/chucklu/p/7122990.html
内容总结
以上是互联网集市为您收集整理的Create the Data Access Layer全部内容,希望文章能够帮你解决Create the Data Access Layer所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。