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