ORM Operation 20141128

Source: Internet
Author: User

Orm
O-object Object
R-relation relationship
M-mapping Mapping

O--M--R

Table name-Class name
Column Name-property name
Table relationships-member objects of a class

LinQ Integrated Query Language sql-Structured Query language
LINQ includes: LINQ to Sql,linq to Object,linq to Dataset,linq to Entity

LinQ to SQL
First step: Establish Linq2sql class
Step Two: Instantiate the context object.
Step Three: Operation

First, Increase:
1. Build the object.
Info data = new info ();
Data. Code = "p211";
Data. Name = "Zhou Qing";
Data. Sex = false;
Data. Nation = "N001";
Data. Birthday = new DateTime (1990, 1, 2);

2. Register the newly created object in the context.
Context.Info.InsertOnSubmit (data);

3. Submit
Context. Submitchange ();

Second, delete:
Mydbdatacontext context = new Mydbdatacontext ();
1. Find
var q = context.Info.Where (p = = P.code = = "P003");

if (Q.count () > 0)
{
Info data = Q.first ();
2. Registration
Context. Work.deleteallonsubmit (data. work);
Context. Family.deleteallonsubmit (data. Family);
Context.Info.DeleteOnSubmit (data);
3. Submit
Context. SubmitChanges ();
}
Third, change:
1. Find
2. Change
3. Submit
Four, check:
Query all
Mydbdatacontext context = new Mydbdatacontext ();
All personnel
var q = context. Info;
Show
foreach (Info data in Q)
{
Data. Nation1: The national object that the current person corresponds to.
Console.WriteLine (data. Name+ "\ t" +data. Nation1.name);
Data. Work: A collection of working records for the current person
foreach in data. Work)
{
Console.WriteLine ("\ T" +work. firm+ "\ t" +work. Depart);
}
}
Query based on PRIMARY key
var q = from P in context. Info where P.code = = "p211" select P;
var q = context.Info.Where (p = = P.code = = "p211"). Where (p = = P.nation1.name = = "Han"); Lambda expression (the most streamlined function)
var q = context.Info.Where (p = = P.code = = "p211" && p.nation1.name== "Han");
All of the above queries return a collection by default.

Querying primary Key objects:
var q = context.Info.Where (p = = P.code = = "p211"); The default is to return the collection
if (Q.count () > 0)//See if the data is found in the collection.
{
Info data = Q.first (); Take the first object out of the
Console.WriteLine (data. Nation1.name + data. Name);
}

ORM Operation 20141128

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.