ArticleDirectory
- Object container Properties
- Object Attributes
- Object Attributes
Programming Entity FrameworkVersion 2Translation Index
The Entity Data Model (EDM) is an application.ProgramAnd data storage. EDM provides the ability to work with the data concept view rather than the actual database architecture. The. net api provided by EF uses EDM for every interaction with data storage, whether it reads or saves data. The EF tool generates classes from these models to ensure that you work with the objects described by EDM.
In this chapter, you will use the Object Data Model Wizard to create a simple EDM, and then check it in the designer and Native XML file. This chapter will be loyal to the basic elements of the model, so that you are familiar with the EDM structure and how the most common elements interact with the otherCodeAnd data.
In Chapter 14th, you will begin to study the most complex aspects of EDM, such as its different inheritance functions and how to customize a model that better reflects the business logic.
Why is the object data model used?
Well-designed data can raise questions to developers.
In the world of data, databases are designed for maintenance, security, efficiency, and scalability. Its data is organized in a way that can meet the needs of a good database design, to provide challenges for developers who want to access the data.
Note: An object data model is a concept. EF has a specific implementation in the design phase, that is, the edmx file. At runtime, The edmx file is divided into three independent XML files. For clarity, this book will discuss the implementation of EF simply involves the EDM or Entity Data Model (or model ). But remember that EDM literally refers to the concept of using some types of models to represent entities in an application.
EDM follows the object link model discussed in chapter 1st, but in EF, it moves the model to the XML file used during EF running.
When EDM is used properly, developers can focus on their business objects, even if they are reading data from a database or persisting it into a database. You, as a developer, do not worry about the data structure, table or view name, stored procedure name, or the parameters they require. You do not need to create the objects used to connect to the database, do not care about the structure of the returned data, and then use the code to convert them into objects.
You only need to work with the conceptual model and code model entity classes. When you do this in the scope of EF, when EF is running, it processes database connections, database command generation, query execution, Object Instantiation, and the details of persistent changes back to the database.
EDM in EF
In the EDM Implementation of EF, the main XML file represents the conceptual model, that is, the actual EDM. The second XML file represents the database architecture, and the third represents the mappings between the first two. In the design phase, all three files are in the edmx file. During the build process, edmx files are separated into three metadata files used for running. EF then provides a framework that allows developers to write. NET applications based on this model.
Note: In chapter 25th, you will learn about future technologies from Microsoft. It will enhance EF, which includes alternatives that can replace the XML architecture.
Since EDM adopts the concept mode, database representation, ing file, and access EF, note that ADO. Net is a provider of the target database, and EF does not care about what the target database is. It provides a common way to interact with the database and send changes back to the database through the query syntax.
Although EF provides developers with rich features, its most important features are as follows:
- By default, it automatically generates classes from the model and dynamically updates these classes at any time when the model changes.
- It takes care of all database connections so that developers no longer have to write code to interact with the database.
- It provides a common query syntax for the query model. Note that it is not a database. Then convert these queries into queries that the database can understand.
- It provides a mechanism for tracking changes to the Model objects used in applications and processes data updates.
In addition, due to the dynamic generation of model classes, minor changes will not have the greatest impact on your applications. In addition, modifying a model is easier than modifying the object and the data access code on which they depend.
What you have to do in this book will depend on EDM, so in order to prepare this, we will create a simple model and place it under the microscope, in this way, you will have a thorough understanding of who EF works.
Walkthrough: Create your first EDM
Let's start by creating a model from a simple database programmingefdb1. This simple database has only two tables, one view and some stored procedures, so it is very suitable for getting started. With EDM, you will be able to browse its elements and their relationships, which will be carried out later in this chapter.
Note: This walkthrough we will use a custom SQL Server database programmingefdb1 where you can download the http://www.learnentityframework.com from the site of this book. Vs2010 provides a connection to SQL Server. As mentioned in chapter 1st, you can install other providers to connect to other databases, such as SQL Server ce, MySQL, Oracle, and vistadb.
- Create a new console application project by selecting the console application project template (see Figure 2-1 ). I name it chapter2consoleapp.
Reminder: Make sure that the project is a. NET Framework 4 Project. You can see the filter menu at the top of the new project window. Many of the features in this book are not available in. Net 3.5. If you choose the wrong one, you will be confused during the learning process.
- In the solution browser, right-click chapter2consoleapp to add a new item, click Add, and then click Add item.
- In the template list, select ADO. NET Entity Data Model and click Add. (See table 2-2 ).
- On the select model content page, select the slave database generation option and click Next.
- On the Select data connection page, select programmingefdb1 from the available connection drop-down list.
NOTE: If no programmingefdb1 is created for the database connection in Vs, click Create connection to open the Connection Properties dialog box and create a new database connection.
- At the bottom of this page, modify the default connection setting name from the default "pgrammingefdb1entities" to "sampleentities", and then click Next.
- On the select your database object page, select the table and view nodes. This will select all tables and views in the database. Similarly, you can expand any node and select the desired object. This database has two tables (contact and address), one view (vofficeaddress) and six stored procedures. For this example, you only need tables and views.
Note: The stored procedure selection box is skipped. In chapter 7th, we will return to the stored procedure.
- At the bottom of this page, modify the model command space to "samplemodel", which is consistent with the connection string command.
- Click Finish.
The new model will be displayed in the designer window, and its file model1.edmx will be available in the solution browser. 2-3.
NOTE: With vs2010, EF also supports model-first design. You can create a model from a draft and then create a database based on this model. We will discuss model-first design in Chapter 25th.
Check EDM in design window
The Entity Designer window is very useful. It allows you to view EDM and its members in diagrams. Otherwise, you will have to deal with a large number of native XML files that make you dizzy. After the illustration, you will have the opportunity to do more things later in this chapter.
After the model is generated through the wizard, the model is opened in the designer view. If you disable it, you can open it again by double-clicking the edmx file in the solution browser. The design view is the default view of the edmx file.
The model1.edmx is displayed in the way of the designer 2-3. It describes the EDM composed of three entities, one contact entity, one address entity, and one vofficeaddress entity. The first two are from the database, and the third is from the view. The designer displays a line between the contact and adress, indicating a one-to-many relationship between them. Each object has a series of attributes, and the related entities also have navigation attributes.
A scalar attribute is a literal attribute of an object's values. For example, a contact object consists of something like contactid, firstname, lastname, and title. These are consistent with the columns in the table.
Navigation properties point to related entities. The contact object has the address attribute, which allows applications to navigate from the contact to the address set related to the contact. The contact attribute of the address object allows you to navigate from address to a single contact associated with the address object.
The line of the connected object indicates the Association, which is the relationship between entities. Pay attention to the end position of the link. In Figure 2-3, it is close to contact. lastname and address. stateprovince, which has no special meaning. A link only connects an object. It does not imply any special attributes.
navigation and association: who is who? bidirectional relationships are usually described as the number of defined entities that can be used to form the shortcut syntax at each end. This is called the number of duplicates at each end. The weight is also known as the relational base, although you won't see it commonly used in EF. the weight options include: 1 (single) * (multiple) 0... 1 (0 or single) the two ends are combined to describe the relationship. for example, "1: *" indicates "one-to-many ". A typical example is an order and multiple product items. "0... 1 "indicates" 0 or 1-to-many ". The example of this relationship is in the middle of the merchant and order. A freight forwarder may transport multiple orders, but may also transport one order. However, the freight forwarder may not have any orders at the beginning. Therefore, in this relationship, it can be 0 or a freight forwarder. |
Note: The object has scalar attributes such as contactid and the contact navigation attribute referenced by contactid. If you have been working with EF of earlier versions, the foreign key contactid is new content. It is optional, but it exists by default. Later in this chapter, you will read more about it.
When working with the object designer, you will see more information about the container, each object, and each object attribute in the attribute window of vs IED.
Object container Properties
The logical group of an entity in a model is called an entity container.
Figure 2-4 shows the Properties window of the object container. Some Properties of the window are collapsed. Here, you can modify the name and Command Space of the window and define the diversified rules of the model and others.
Object Attributes
Each entity of EDM is associated with each other, and the model itself has attributes. Let's take a look at some attributes of the Contact entity in the model we just created.
Select the contact object to view its Properties window (see Figure 2-5 ).
In the Properties window, you can see that the object not only has the name "Contact" from the database table name, but also has the object setting name attribute. If the table name in the database is a complex number, for example, contacts, the wizard still regards the named object as contact because the object name should be a singular number.
An object set is a container of a single object set. Therefore, the object set name "contacts" contains the set of contact objects. By default, the wizard will use the plural object name when creating an object name. In the Object Data Model Wizard, you can modify this behavior by not selecting "plural or singular when creating an object name.
Object Attributes
Figure 2-6 shows the firstname attribute of the contact. As you can see, for example, firstname is a string that cannot be null (nullable is false) (type is string ).
Note: Properties describing the object property architecture, such as fixed length, is also known as attributes. Because it is discussing the troubles caused by "attribute attributes", I will specify it as attributes.
Unicode, max length, and fixed length attributes are ignored during EF running. Do not expect EF to automatically perform verification based on these attributes. These attributes are used by other EDM users, such as ASP. net MVC 2.0 and ASP. NET Dynamic Data Control, And the storegeneratedpattern attribute that you will learn in Chapter 25th to generate database scripts. You can use them when using EF at a low level. For example, this is the same as metadataworkspce, which will be discussed in chapter 21.
Although you can use the designer to do more, it is time to open its native format model. You will find additional discussions about Native XML in appendix C. Before proceeding to the next step, remember to save all your current work.
Note: In chapter 25th, when creating a model from a draft in the designer, you will learn a series of features of the entity, their attributes, associations, and more.