Entity Framework 6

Source: Internet
Author: User
Tags connectionstrings

Find the corresponding instance and create the first efcodefirst instance.

 

1: Create a codefirstnewdatabasesample console application

2. Obtain the object framework. For more information, see

Https://docs.microsoft.com/zh-cn/ef/ef6/fundamentals/install

3: Add the following code



 

 

Using system;
Using system. Collections. Generic;
Using system. Data. entity;
Using system. LINQ;
Using system. text;

Namespace codefirstnewdatabasesample
{
Class Program
{
Static void main (string [] ARGs)
{
Using (var db = new bloggingcontext ())
{
// Create and save a new blog
Console. Write ("enter a name for a new blog :");
VaR name = console. Readline ();

VaR blog = new blog {name = Name };
DB. Blogs. Add (blog );
DB. savechanges ();

// Display all blogs from the database
VaR query = from B in db. Blogs
Orderby B. Name
Select B;

Console. writeline ("all blogs in the database :");
Foreach (VAR item in query)
{
Console. writeline (item. Name );
}

Console. writeline ("press any key to exit ...");
Console. readkey ();
}
}


}

Public class blog
{
Public int blogid {Get; set ;}
Public string name {Get; set ;}

Public Virtual list <post> posts {Get; set ;}
}

Public class post
{
Public int postid {Get; set ;}
Public String title {Get; set ;}
Public String content {Get; set ;}

Public int blogid {Get; set ;}
Public Virtual blog {Get; set ;}
}

Public class bloggingcontext: dbcontext
{
Public bloggingcontext (): Base ("blogcontext "){}

Public dbset <blog> blogs {Get; set ;}

Public dbset <post> posts {Get; set ;}

}
}

 

This is the configuration file

 

 

<? 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>
<Entityframework>
<Defaconnecticonnectionfactory type = "system. Data. entity. Infrastructure. sqlconnectionfactory, entityframework"/>
<Providers>
<Provider invariantname = "system. Data. sqlclient" type = "system. Data. entity. sqlserver. sqlproviderservices, entityframework. sqlserver"/>
</Providers>
</Entityframework>

<Connectionstrings>
<Add name = "blogcontext"
Providername = "system. Data. sqlclient"
Connectionstring = "Server =.; database = blogging; Integrated Security = true;"/>
</Connectionstrings>


 
</Configuration>

 

 

 

The Code is as follows:

 

Link: https://pan.baidu.com/s/1lEZeprJB4y3_APKrIYD1oA
Extraction code: bvbo
Copy the content and then open the Baidu online storage mobile app.

 

Entity Framework 6

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.