C # NPOCO lightweight ORM framework (Getting Started ),

Source: Internet
Author: User

C # NPOCO lightweight ORM framework (Getting Started ),

Currently, this framework cannot be used to search Chinese documents.

Only the English wiki is available. Take an excerpt from translation learning.

Most of them are machine flip, and some places have been changed,

Because bloggers are also low-level, and the red ones are blind to understanding.

There may be errors. You can point them out if you find them.

Wiki address: http://github.com/schotime/NPoco/wiki

 

First query:

 1 public class User  2 { 3     public int UserId { get;set; } 4     public string Email { get;set; } 5 } 6  7 using (IDatabase db = new Database("connStringName"))  8 { 9     List<User> users = db.Fetch<User>("select userId, email from users");10 }

Note: DatabaseYou need to disable the connection (which can be used as your object.

This is the name of the attribute mapped by the column name.UserObject. This is case-insensitive.
No ing (query) is used ).

 

Ing

 

Ing is not required by default. If this attribute is not specified, the table name is assumed to be a category name and the primary key is "ID ".

The most common basic property ing is:

Demo:

 1 [TableName("Users")] 2 [PrimaryKey("UserId")] 3 public class User 4 { 5     public int UserId { get;set; } 6     [Column("emailAddress")] 7     public string Email { get;set; } 8     [ResultColumn] 9     public string ExtraInfo { get;set; }10     [Ignore]11     public int Temp { get;set; }12 }

 

Query a single object

 

You can query an object from a database in several different ways.

ID

The simplest way is to useSingleById<T>()Method.

1 IDatabase db = new Database("connStringName");2 User u = db.SingleById<User>(3);
Use SQL

As you can see below, there are only rules. If you do not explicitly provide the selected terms, they will be automatically generated for you and will be attached to them.

1 User u = db.Single<User>("where emailaddress = @0", "email@domain.com");2 or3 User u = db.Single<User>("select u.* from users u where emailaddress = @0", "email@domain.com");

Both methods have an "ordefault" method. If you are not sure about the existence of the object. If it does not exist and you do not use ordefault, an exception is thrown.

AndFirst<T> AndFirstOrDefault<T> If multiple data entries exist, an exception is thrown. If no more than one record is returned.

 

Insert, update, Delete Insert new records
1 IDatabase db = new Database("connStringName");2 User u = new User() 3 {4     Email = "name@domain.com",5     LastLoggedIn = DateTime.UtcNow6 };7 8 db.Insert(u);
Update record
1 var user = db.SingleById(1);2 user.Email = "new@domain.com";3 db.Update(user);
Delete record
1 var user = db.SingleById(1);2 db.Delete(user);3 or4 db.Delete<User>(1);
Overwrite or create a record
1 IDatabase db = new Database("connStringName");2 User u = new User() 3 {4     Email = "name@domain.com",5     LastLoggedIn = DateTime.UtcNow6 };7 8 db.Save(u);

This inserts a new record or updates an existing record. Whether it exists is determined by the primary key.

 

Query list

 

Get all:

1 List<User> users = db.Fetch<User>();

Standard Acquisition:

1 List<User> users = db.Fetch<User>("where isActive = 1");

Use SQL:

1 List<User> users = db.Fetch<User>("select u.* from users where u.isActive = 1");
Laziness

Warning:Follow these steps:Query<T>Use generated keywords. All running results of this query are traversed. If you do not fully understand this concept, useFetch<T>.

1 List<User> users = db.Query<User>("select u.* from users where u.isActive = 1");

 

Query page

There are two main methods for querying pages.

Page <T>
1 IDatabase db = new Database("connStringName");2 Page<T> pagedUsers = db.Page<User>(2, 10, "select u.* from users u order by userid");

WherePage<T>It is defined:

1 public class Page<T> 2 {3     public long CurrentPage { get; set; }4     public long TotalPages { get; set; }5     public long TotalItems { get; set; }6     public long ItemsPerPage { get; set; }7     public List<T> Items { get; set; }8 }

Note: You must provide an order by statement in the SQL statement to know the order in which you want to paging data.
The first parameter of the page is the page number. This number starts from 1 on the first page. The second parameter is the page size. In the preceding example, the second page containing 10 users is returned.

SkipTake <T>

The SkipTake <T> method is very similar to the Skip method in LINQ. It has the same number of parameters as the page <T> method, but the first parameter is not the page number, but the number of records (that is, the number of records to be retrieved ?). The second parameter is the number of records returned after skipping x records (that is, the starting position ?). To return the same results as the Page <T> method, the query is as follows:

1 List<User> users = db.SkipTake<User>(10, 10, "select u.* from users u order by userid");

 

Transaction support

 

Example 1
1 using (IDatabase db = new Database("connStringName")) 2 {3     db.BeginTransaction();4     //Your CRUD operation here5     db.CompleteTransaction();6 }
Example 2
1 using (IDatabase db = new Database("connStringName")) 2 {3     using (var transaction = db.GetTransaction())4     {5         //Your CRUD operation here6         transaction.Complete();7     }8 }

 

Translation time:

Related Article

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.