Dnetorm a lightweight ORM framework

Source: Internet
Author: User
Tags connectionstrings

Introduction to use

1. configuration file configuration Good connection string

The value of the connectionname corresponds to the name of the connection string

<appSettings> <add key="ConnectionName"Value="DB"/> </appSettings> <connectionStrings> <!--<add name="DB"connectionstring="Data Source=localhost/xe; User id=hr; PASSWORD=HR;"Providername="oracle.manageddataaccess"/>--> <!--<add name="DB"connectionstring="Host=localhost;database=test;uid=root;pwd=sa123456;charset=utf8;"Providername="MySql.Data.MySqlClient"/>--> <add name="DB"connectionstring="Data source=localhost; Initial catalog=test; Persist Security info=true; User Id=sa; password=123456;"Providername="System.Data.SqlClient"/></connectionstrings>

2. Define a good entity

  Public classBook {[Key] [databasegenerated (databasegeneratedoption.identity)] Public int? BookID {Get;Set; }  Public stringBookName {Get;Set; }  Public int? Authorid {Get;Set; } [Notmapped] Public stringAuthorName {Get;Set; }  Public Double? Price {Get;Set; }  PublicDatetime? publishdate {Get;Set; } }

Additions and deletions are currently supported by SQL Server, MySQL, Oracle


            using New Dnetcontext ())            {                var authorid = db. ADD (new" Zhang San "+true  });                Db. ADD (new" from getting started to giving up "20.5, publishdate = DateTime.Now, Authorid = Authorid});            }


            using New Dnetcontext ())            {

int Authorid = db. Getmax<author> (M = (int) m.authorid); Db. Update" Demon Hunter ", m = = M.authorid = = Authorid); }


            using New Dnetcontext ())            {                vartrue, q = Q.orderby (M = m.authorid));                 var effect = db. Delete (author);                 int Authorid = db. Getmax<author> (M = (int) m.authorid);                Db. Delete<Author> (m = M.authorid = = Authorid);            }


Single-Table Query

The following SQL formats are supported


Contains, StartsWith, EndsWith


Contains Collection Class

Upper Lower

ToUpper, ToLower

SQL Character Size comparison

CompareTo, Equals

Charindex InStr


Is null or = ' '


Type conversions, time formatting


Integer conversions



TrimStart, TrimEnd, Trim

            using(Dnetcontext db =NewDnetcontext ()) {                varAuthor = db. getsingle<author> (M =true, q = Q.orderby (M =M.authorid)); Author= db. getsingle<author> (M = M.authorname.contains ("John Doe") && M.isvalid = =true); varAuthors= db. getlist<author> (M = M.authorname.startswith ("Zhang San") && M.isvalid = =true); //Get dynamic typelist<Dynamic> name = db. getdistinctlist<author> (M = M.authorname.startswith ("Harry") && M.isvalid = =true,m=>m.authorname); List<string> name1 = db. Getdistinctlist<author,string> (M = M.authorname.startswith ("Harry") && M.isvalid = =true, M =m.authorname); //Get maximum value                varAuthorid = db. Getmax<author> (M = (int) M.authorid); //Dynamic QueryWherebuilder<author>where=NewWherebuilder<author>(); where. and (M=>m.authorname.contains ("Zhang San")); where. and (M = m.authorid==3); where. Or (m=>m.isvalid==true); Db. GetList<Author> (where.                Whereexpression); //paging parameters coming from the front deskPagefilter page =NewPagefilter {pageindex=1, pagesize=Ten }; Page. and<Author> (m=>"The Watcher's Sky".                Contains (M.authorname)); Page. by<Author> (Q=>q.orderby (m=>m.authorname). OrderByDescending (m=>M.authorid)); Pagedatasource<Author> pagesource= db. Getpage<author>(page); }

Multi-Table Query

            using(Dnetcontext db =NewDnetcontext ()) {                varBooks = db. Joinquery.leftjoin<book, Author> ((m, n) = M.authorid = = N.authorid && N.isvalid = =true)                     . fields<book, Author> ((m, n) =New{m, n.authorname}). ORDERBYASC<Book> (M =m.bookname). Select<Book>(); varJoin = db. Joinquery.leftjoin<book, Author> ((m, n) = M.authorid = = N.authorid && N.isvalid = =true)                     . fields<book, Author> ((m, n) =New{m, n.authorname}). ORDERBYASC<Book> (M =m.bookname); Pagefilter page=Newpagefilter {PageIndex =1, PageSize =Ten};//paging parameters The front desk came .Join. Selectpage<book>(page); }

Support for transactions

            using(Dnetcontext db =NewDnetcontext ()) {db.                Database.begintransaction (); Try{List<Author> authors =NewList<author>();  for(inti =0; I <= -; i++) {authors. ADD (NewAuthor {authorname ="Test"+ i.tostring (), age = -, IsValid =true }); } db.                    ADD (authors);                Db.DataBase.Commit (); }                Catch{db.                Database.rollback (); }            }

Support for SQL statement queries

            using(Dnetcontext db =NewDnetcontext ()) {StringBuilder SQL=NewStringBuilder (); List<DbParameter> parameters =NewList<dbparameter>(); Sql. AppendFormat (@"SELECT {0},a.authorname from book B left joins Author A on A.authorid=b.authorid WHERE", Sqlbuilder.getselectallfields<book> ("B")); Sql. Append ("b.bookid> @BookID"); Parameters. ADD (db. Getdbparameter ("BookID",1)); Pagedatasource<Book> books = db. getpage<book> (SQL. ToString (),NewPagefilter {pageindex=1, pagesize=5}, Parameters.                ToArray ()); List<Book> BKS = db. Getlist<book>(SQL. ToString (), parameters.            ToArray ()); }

Welcome to the technical discussion

Latest code Download


Dnetorm a lightweight ORM framework

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.