EF Framework Step by step (4)-dbcontext apply to existing databases

Source: Internet
Author: User
Tags connectionstrings

EF4.1 has three ways to perform data manipulation and persistence. Are Database-first,model-first,code-first, the front has been briefly introduced. Here's a brief summary:
1. Database First is based on existing databases, using some tools (such as the EF Designer provided by VS) to create entity classes, matching relationships between database objects and entity classes, and so on, you can also manually modify these automatically generated code and matching files.
2.Model First This approach is to use some tools (such as the EF Designer vs) to design the Entity Data model and their relationship, and then based on these entities, relationships to generate database objects and related code files.
3. Code First this way you need to write something like a physical object, a data relationship, and then automatically create a data object based on an existing code description, which has been briefly stated in the previous article. In fact, this approach is very similar to model first. The code we write ourselves is actually a code representation of the entity model, and model first describes the entity model in a visual way.

The code first method is by default a namespace. DbContext class name as the name of the automatically created database, as in the previous article, Codefirstsample.blogdbcontext.

We can also specify the database name as follows:

    public class Blogdbcontext:dbcontext
{
<summary>
Create a new database and name it blogdb,
</summary>
Public Blogdbcontext (): Base ("Blogdb") {}

Public idbset<bloguser> blogusers {get; set;}
Public idbset<post> Posts {get; set;}


}

Code first can also be used in databases that already exist, in the following ways:

First step: Configure the database connection string in app. Config or Web. config

<configuration>
<connectionStrings>

connectionstring= "Data source=.\sqlexpress;initial catalog=myblogdb;integrated security=true"/>
</connectionStrings>
</configuration>

The second step is to get dbcontext to get the existing database, specifying the use of this connection string when constructing DbContext

    public class Blogdbcontext:dbcontext
{

<summary>
Finds the Blogdb section in the App. Config or Web. config file as a connection string
However, the table must already exist in the database, that is, the table will not be recreated, and this method resembles database first
</summary>
Public Blogdbcontext ()
: Base ("Name=blogdb")
{ }
Public idbset<bloguser> blogusers {get; set;}
Public idbset<post> Posts {get; set;}


}

Step three: Add the classes to match and the matching relationships.

    public partial class Bloguser
{

public int Bloguserid {get; set;}
public string Blogname {get; set;}
Public virtual icollection<post> Posts {get; set;}
}

public partial class Post
{

public int PostID {get; set;}
public string Posttitle {get; set;}
public int Bloguserid {get; set;}
Public virtual Bloguser Bloguser {get; set;}
}

Fourth step: Get the data

         using (Blogdbcontext db = new Blogdbcontext ())
{

Find users by primary key
Bloguser bloguser = db. Blogusers.find (4);
Console.WriteLine (Bloguser.blogname);
foreach (var item in bloguser.posts)
{
Console.WriteLine ("\t{0}", item. Posttitle);
}
}

In fact, this is the database first, instead of the class and XML file generated by the visualizer using the classes we write ourselves.

EF Framework Step by step (4)-dbcontext apply to existing databases

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.