Entity Framework learning notes (2) Data Model use process, entityframework

Source: Internet
Author: User

Entity Framework learning notes (2) Data Model use process, entityframework

Use Entity Framework data Model creation:

Development Environment: VS2012

Database: SQL Server 2008

Entity Framework Version: 6.12

The following figure shows the new project architecture: (of course, this is my project architecture, just for reference)

 

1. Create a project

Create a console project: Future. LifeWillBetter. DAL. ForModel. leleapplication

2. Create a data model

Right-click Future. LifeWillBetter. DAL. ForModel. leleapplication console application

Add-new project-ADO. NET Entity Data Model-empty EF designer model-completed (for example)

So far, my data model has been created. Name: FutureLifeWiilBetterModel. edmx

3. Add an entity model (Open FutureLifeWiilBetterModel. edmx)

3.1 add an object

In FutureLifeWiilBetterModel. edmx, right-click the blank area and choose "add"> "entity"> "OK ".
Enter the object name, modify the object set name (this is the name of the data table in the database), and modify the attribute name
Right-click the object and choose "add"> "add"> "scalar property". Add the desired field and modify the attribute of the field.

In the same way, you can add multiple entities.

3.2 add Link
In FutureLifeWiilBetterModel. edmx, right-click in the blank space and choose "Add-" Join-"to set the desired link-" OK ".

3.3 generate Database

3.3.1 In FutureLifeWiilBetterModel. in edmx, right-click in the blank space-> Generate Database Based on Model-> create a connection (set database connection)-> OK-> next (the SQL statement code is generated at this time)->.

 

Click Next. An SQL script is generated after the task is completed.

 

 

I found that there is only one T_Users.cs entity class under FutureLifeWiilBetterModel. tt in my FutureLifeWiilBetterModel. edmx data model. This is a double-click to open the FutureLifeWiilBetterModel. edmx data model.

Since this is created based on the VS data model, I click "generate Database Based on Model". This will generate an SQL script again, continue to execute the script following the above steps, and then refresh the Future. lifeWillBetter. DAL. forModel. consoleApplication console program, this is T_UserStates.cs, it is also a synchronization process!

 

3.4 file Introduction

3.4.1 The App. config file is generated after the FutureLifeWiilBetterModel. edmx model is created,

<?xml version="1.0" encoding="utf-8"?><configuration>  <configSections>    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />  </configSections>  <startup>    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />  </startup>  <connectionStrings>    <add name="FutureLifeWiilBetterModelContainer" connectionString="metadata=res://*/FutureLifeWiilBetterModel.csdl|res://*/FutureLifeWiilBetterModel.ssdl|res://*/FutureLifeWiilBetterModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=Future.LifeWillBetter2;user id=sa;password=173007740;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />  </connectionStrings>  <entityFramework>    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />    <providers>      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />    </providers>  </entityFramework></configuration>

3.4.2FutureLifeWiilBetterModel.Context.tt FutureLifeWiilBetterModel. Context. cs this is an object Context class

// -------------------------------------------------------------------------------- // <Auto-generated> // This code has been generated from the template. //// Manual modification of this file may cause unexpected behavior of the application. // If the code is re-generated, the manual changes to the file will be overwritten. // </Auto-generated> // -------------------------------------------------------------------------------- namespace Future. lifeWillBetter. DAL. forModel. leleapplication {using System; using System. data. entity; using System. data. entity. infrastructure; public partial class failed: DbContext {public partition (): base ("name = partition") {} protected override void OnModelCreating (DbModelBuilder modelBuilder) {throw new UnintentionalCodeFirstException ();} public virtual DbSet <T_Users> T_UsersSet {get; set;} public virtual DbSet <T_UserStates> T_UserStatesSet {get; set ;}}}

3.4.3 FutureLifeWiilBetterModel. tt contains the data entity class, which is also an entity class in the unit of database tables)

Complete!

 

 

 

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.