EF Framework Step by step (1)-database-first

Source: Internet
Author: User
Tags static class

The ADO Framework is an object-relational (O/R Mapping) solution developed by Microsoft based on ADO and is now included in Visual Studio Service Pack 1 and. NET Released in the Framework 3.5 Service Pack 1. Currently the latest version is 4.1, you can click here to download.

The net entity Framework is dominated by the Entity Data Model (EDM), which divides the data logic layer into three blocks, namely the conceptual schema, Mapping schema and Storage schema three Layer, which also has Entity client,object Context and LINQ can be used. In the latest 4.1 release, the addition of the DbContext API and the Codefirst,dbcontext API is a simple API based on ObjectContext and some other types of abstraction, and is optimized. Code first is a new development model based on the Entity Framework, which originally had only database and model two. Code first uses C#/VB. NET class to define the model and then map to an existing database or generate a new database structure. Code first also supports custom configurations via the data annotations or the fluent API. Too much content is not much to say, under the frame chart:

A brief introduction to this is the first to build a simple example to feel the framework. This example uses a simple Database-first method.

First build a sample database Blogdb, and then create two tables as shown:

Randomly enter some test data.

Step two: Open VS2010 and create a new console application Efsample

The third step: Right-click on the project directory, in the pop-up menu to add a new item, add the ADO Data Model, and named: BLOG.EDMX, as shown:

  

In the new window that pops up, select Generate from database, next

  

Select the data connection and save the database connection string in the program, and continue to the next step

  

Select the table to add, complete, then add this blog.edmx, this file is automatically generated from the database model file, based on the XML format, including the conceptual model, the storage model, and the mapping between the two models. As shown:

  

This is the last generated EDM.

  

Finally, open the Program.cs type code, as shown below.

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Namespace Efsample
{
Class Program
{
static void Main (string[] args)
{
ilist<bloguser> blogusers = Testef.getallblogusers ();

Traverse all Query Results
foreach (Var bloguser in blogusers)
{
Console.WriteLine (Bloguser);

Get the contents of a foreign Key object
foreach (Var post in bloguser.posts)
{
Console.WriteLine ("\ t essay title: {0}", post.) Posttitle);
}
}
}
}

public static Class Testef
{
public static ilist<bloguser> Getallblogusers ()
{
Blogdbentities is inherited from the ObjectContext class and is automatically generated
can open a. Blog.Desgner.cs file to find
Prototype: public partial class Blogdbentities:objectcontext
It can be understood that he represents the current database environment object
At the same time, two entities bloguser and post can be found in the Blog.Desgner.cs.
Blogdbentities blogdb = new Blogdbentities ();
reading data using LINQ syntax
ilist<bloguser> blogusers = blogdb.blogusers.tolist<bloguser> ();
return blogusers;
}
}

public partial class Bloguser
{
public override string ToString ()
{
return string. Format ("number: {0} name: {1}", this.) BlogID, this. Blogname);
}
}
}

EF Framework Step by step (1)-database-first

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.