The Entity Framework Code First

Source: Internet
Author: User
Tags connectionstrings

The EF (Entity Framework) is the object-relational correspondence developed by Microsoft on the basis of ADO (O/R Mapping (Object-relational mapping, or ORM, is a technique for solving the mismatch between object-oriented and relational databases.) relationalmapping )) solution.

The Entity Framework leverages the abstraction of data structures to convert each database object into an Application object (Entity), and the data fields are converted to properties, and the relationships are converted to associative attributes (Associat ION), so that the E/R model of the database is completely turned into an object model. It has three generation modes, the database first, model first, and codefirst, so today we're implementing code first. The so-called code first means that you can create the database directly with the codes and do the EF mapping.

First of all, let's add two entity classes, the code is as follows:

Order class

Public  class OrderInfo    {       [Key] public       int Id {get; set;}        public string Content {get; set;}        <summary>//foreign key///       </summary> public       int CustomerId {get; set;}        Public customer customer {get; set;}    }


Customer class

public class Customer    {       [Key] public       int Id {get;set;}        public string Cusname {get; set;}        Public virtualicollection<orderinfo> Order {get; set;}    }

Here's [Key] is the meaning of the identifier, which is to set the primary key, here to add a reference System.ComponentModel.DataAnnotations

Next we'll customize a database context

Public Classhoteldbcontext:dbcontext    {public       hoteldbcontext ()           : Base ("Name=conncodefirst")        {} Public        dbset<customer> Customer {get; set;}         Public dbset<orderinfo> orderinfo{get; set;}    }


To be able to inherit the DbContext class, add System.Data.Entity, which refers to the Packages\entityframework.5.0.0\lib\net40\entityframework.dll in our program directory

Write down the configuration file below.

<?xmlversion= "1.0" encoding= "Utf-8"?><configuration>  <connectionStrings>    <add name= "Conncodefirst" connectionstring= "server=.; Uid=sa;pwd=1;database=codefirstdemodb "providername=" System.Data.SqlClient "/>  </connectionStrings> </configuration>


Note that the string variables we name must be consistent with the variable names in the database context class.

Client programs

Class program    {        static void Main (string[] args)        {            Hoteldbcontext dbcontext = Newhoteldbcontext ();           DbContext. Database.createifnotexists ();//If the database does not exist, create a database        }    }


Execute and see if your database has been added successfully!

Postscript

Learning EF from the code building to practice there will be more different experience, logic is also more clear some, three kinds of construction methods we should go to practice, so as to learn EF .

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Code First

of the Entity framework

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.