Entity Framework tutorial -- create an object data model, entityframework

Source: Internet
Author: User

Entity Framework tutorial -- create an object data model, entityframework

Create an object data model:

This article will show you how to create an EDM SchoolDB database and understand the infrastructure module.

The Entity Data Model (EDM) is a model used to describe the relationship between entities. The following uses Visual Studio 2012 and EF6 to create a SchoolDB database.

1. Open Visual Studio 2012 and create a Console Project

Project-> properties, make sure that the target framework is. NET Framework 4.5.

2. Add the EDM file, right-click the project, and choose add selected ADO. NET Entity Data and name it 'school'

3. the EDM wizard appears, which has four options: EF Designer from database (database priority) and Empty EF Designer model (model priority ), empty Code First model and Code First from database (Code priority function ). In this basic tutorial, only focus on database priority, so select the EF Designer from database option and click Next.

4. You can select an existing database connection or create a new connection. Select an existing database to connect to the SchoolDB database. This will add the corresponding connection string to the app. config file and click Next.

5. In this step, select the EF version. Here, select EF6.0.

Note:If you have installed the latest EF environment, this step will not appear.

6. This step shows all the tables, views, and stored procedures of the SchoolDB database. Select the tables, views, and stored procedures you want to model. You can modify the namespace if needed.

Note:

Pluralize or singularize generated object names OptionIndicates that the attributes of the created object are in the singular and plural form. For example, if the SchoolDB database has a table named Students, the generic parameters (T) in the entity set created by the table (such as DBSet <T>) are created in the singular form. Another example is that the relationship between tables has one-to-many or many-to-many relationships, such as the many-to-many relationship between Student and Course, the Student object contains a set object with the attribute name "Courses" in the plural form.

The second option,Include foreign key columns in the model,Include the foreign key in the model. For example, the Standard table and the Student table have a one-to-many relationship, so multiple Student are associated with a Standard. In the entity model, Student will include the StandardId scalar attribute and the Standard navigation attribute. If this item is not selected, it will only include the Standard navigation attribute, but not the StandardId scalar attribute.

The third option,Import selected stored procedures and functions into entity model,This option is usually required to automatically create stored procedures and functions.

7. After you click "Finish", a file named "School. edmx" will appear in your project. Double-click the file to open the EDM designer, which will display all the tables you just selected and their relationships.

EDM also adds the connection string to the config file.

 1 <?xml version="1.0"?> 2 <configuration> 3   <configSections> 4     <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 5     <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/> 6   </configSections> 7   <startup> 8     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/> 9   </startup>10   <entityFramework>11     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>12     <providers>13       <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>14     </providers>15   </entityFramework>16   <connectionStrings>17     <add name="SchoolDBEntities" connectionString="metadata=res://*/SchoolDB.csdl|res://*/SchoolDB.ssdl|res://*/SchoolDB.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\sqlexpress;initial catalog=SchoolDB;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient"/>18   </connectionStrings>19 </configuration>
View Code

Now, you have created a simple EDM model through an existing database.

Next, let's take a look at what files EDM (School. edmx) has created for us.

Object table ing:

Each object is mapped to the corresponding data table. You can right-click the object and view the ing relationship. Of course, if you modify the attribute name of an object in the EDM designer, the table ing will automatically apply the change.

Context and object class:

Each object model creates a Context Class and several entity classes corresponding to the table you selected are displayed in EDM.

School. Context. tt:

This is the context class generated by the T4 template file. You can open the. edmx file and open it to create a class named {database name} + "Entities" by default. This class inherits the DBContext class (this class is defined in EF, and ObjectContext class before EF5.0)

School. tt:

This is the entity object class generated by the T4 template. The following code snippet shows the Student object.

public partial class Student{    public Student()    {        this.Courses = new HashSet<Course>();    }        public int StudentID { get; set; }    public string StudentName { get; set; }    public Nullable<int> StandardId { get; set; }    public byte[] RowVersion { get; set; }        public virtual Standard Standard { get; set; }    public virtual StudentAddress StudentAddress { get; set; }    public virtual ICollection<Course> Courses { get; set; }}
View Code

EDM Designer:

The EDM designer represents your conceptual model, which represents the relationship between object objects and them. At first, it looks exactly the same as your database table structure, but you can add, merge, and delete fields not required by your application from the designer. You can even add a new object from different database tables to the context menu in this model, as shown above. Remember to map any changes to the storage model. Therefore, you must be careful when making changes.

You can open the EDM designer in XML mode, so that you can see three parts: Concept mode (CSDL), storage mode (SSDL), and msing table (MSL ), all three parts are in XML.

Right-click School. edmx and choose open mode.

Select "XML (text) Editor"

Visual Studio cannot open the model design appearance and XML editor at the same time, so you will see a window to close the model design appearance. Click Yes to see the XML editor format, and then you will see the following interface.

You can see the content of SSDL, CSDL, and C-S mapping, which you can expand to browse. You do not need to write XML data directly, because you can browse and edit the data directly in the model.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.