Framework-code fisrt opening of the ADO Entity (i)

Source: Internet
Author: User
Tags connectionstrings

Framework-code fisrt opening of the ADO Entity (i)

2012-12-25 15:13 by Easy Code, 911 reading, 0 reviews, Favorites, compilation

The ADO framework is a set of entity mapping frameworks for Microsoft. When the EF4.1 (Entity Framework) was released, a code-first design concept was proposed (the comes first, the rest follows). Specific benefits which is more, check the information.

Reference: Programming Entity Framework Code first.pdf

Development environment: VS2010

Development version: ADO Entity Framework 4.1

Download Link: Http://download.microsoft.com/download/0/7/A/07AC6336-D665-4442-B841-39D11BBF2563/EntityFramework41.exe

Reference DLL: Method One: Install the downloaded EXE file, there is a EntityFramework.dll file in the installation file. The DLL file needs to be referenced in the project.

Method Two: Create a new project in VS2010, select Add Libraray package Reference at the reference, select online on the left, and search for the entity Framework installation.

Here is the quick start of code FISRT.

1 Create a new console item QuickStart. Add a model folder with the following classes of files:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Data.Entity;
Namespace Quickstart.model
{
<summary>
Unified Dictionary Table
</summary>
public class Dictionary
{

public string Dictionaryid {get; set;}
public string Dictionaryvalue {get; set;}
public string ParentID {get; set;}
public string Parameter {get; set;}
Public DateTime LastUpdateTime {get; set;}
public string Remark {get; set;}
}
}

The dictionary table holds the category information for each item, and the dictionary table classification and item are a one-to-many relationship

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.ComponentModel.DataAnnotations; Data annotations (need to add the 4.0 version)
Namespace Quickstart.model
{
public class Item
{
public string ItemID {get; set;}
public string Name {get; set;}
Public decimal price {get; set;}
Public Dictionary ItemType {get; set;}
}
}

2 Add a dbcontextapi inherits from the DbContext class,

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Data.Entity; Add Reference
Using Quickstart.model;
Namespace QuickStart
{
public class Dbcontextapi:D Bcontext
{
<summary>
Define the link string used in the configuration file by using the constructor name= "Orderdb"
<para> Use default, Name= "Dbcontextapi" class name </para>
</summary>
Public Dbcontextapi (): Base ("Orderdb") {}

Public idbset<item> Items {get; set;}
Public idbset<dictionary> Dictionarys {get; set;}
}
}

3 Add an app. Config profile

<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<connectionStrings>
<add name= "Orderdb" providername= "System.Data.SqlClient"
Connectionstring= "server=.; Uid=sa;pwd=123456;database=orderdb "/>
</connectionStrings>
</configuration>

4 Add the following code to the main function:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using Quickstart.model;
Using Microsoft.SqlServer.Server;
Namespace QuickStart
{
Class Program
{
static void Main (string[] args)
{
Test and automatically create a database table model
CreateDatabase ();
Console.readkey ();
}

private static void CreateDatabase ()
{
using (var db = new Dbcontextapi ())
{
var dict = new Dictionary ()
{
Dictionaryid = "20121225001",
Dictionaryvalue = "Notebook Computer",
Parameter = "",
ParentID = "ItemType",
Remark = "Notebook computer category key",
LastUpdateTime = DateTime.Now
};
Db. Dictionarys.add (DICT);
int result = db. SaveChanges ();
Console.WriteLine ("Append {0} records succeeded!", result);
}
}
}
}

5 Run the program, after success, will automatically create a database table structure in the database.

Item table

OK, Finish!

Framework-code fisrt opening of the ADO Entity (i)

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.