FluentData micro ORM and fluentdataorm
Recently, I am helping a friend develop a simple management system. I am too lazy, but the NHibernate used for this project is too impractical. I simply use Baidu's micro-ORM, fluentData is the first word to jump into my eyes. A brief understanding of FluentData usage, mainly to see if it meets the requirements of the author. Hmm ...... Hmm ...... The response is not bad. The API is also very simple to use. There are parameterized queries, pages, and so on. Here is the question of this Article. how to use it.
1. Download
Http://fluentdata.codeplex.com/
FluentData requires the support of. NET4.0 and dynamic. The supported databases include:
- Ms SQL Server
- Ms SQL Server Compact 4.0
- SQL Azure MS
- MS Access
- Oracle
- MySQL
- SQLite
- PostgreSQL
- IBM DB2
- Sybase
2. Use
Simple addition, deletion, modification, and query. Create and initialize an IDbContext. The first parameter in the function is the database link string, and the second parameter is the database type.
public static IDbContext Context() { var context = new DbContext().ConnectionString(ConnectionString, new SqlServerProvider()); return context; }
New use:
Var MembershipId = FluentDataHelper. context (). SQL ("insert into Membership (Name, Address) values (@ 0, @ 1 )"). parameters ("Zhang San", "Changsha "). executeReturnLastId <int> ();
Or
var Insert = FluentDataHelper.Context().Insert("Membership") .Column("Name", Name) .Column("Address", Address);
if (ImageByte != null) Insert.Column("Image", ImageByte); int MembershipId = Insert.ExecuteReturnLastId<int>();
Delete and use:
int RowId = FluentDataHelper.Context().Sql("delete Membership where Id = @Id").Parameter("Id", Id).Execute();
Or
int RowId = FluentDataHelper.Context().Delete("Membership").Where("Id", Id).Execute();
Modify and use:
int RowId = FluentDataHelper.Context().Sql("update Membership set Name = @Name where Id = @Id").Parameter("Name", Name).Parameter("Id", EditId).Execute();
Or
var Update = FluentDataHelper.Context().Update("Membership") .Column("Name", Name) .Where("Id", EditId); if (ImageByte != null) Update.Column("Image", ImageByte); int RowId = Update.Execute();
Query usage:
// Returns a single int RowId = FluentDataHelper. context (). select <int> ("Count (1 )"). from ("Membership "). where ("Tel = @ Tel "). parameter ("Tel", Tel ). andWhere ("Id! = @ Id "). Parameter (" Id ", EditId). QuerySingle ();
// Returns the object MembershipEntity Entity = FluentDataHelper. context (). select <MembershipEntity> ("*"). from ("Membership "). where ("Id = @ Id "). parameter ("Id", EditId ). querySingle ();
// Return multiple objects List <MembershipGroupEntity> ListGroup = FluentDataHelper. Context (). Select <MembershipGroupEntity> ("*"). From ("MembershipGroup"). queryentity ();
// Multi-Table Association and paging var select = FluentDataHelper. context (). select <VehicleEntity> ("Vehicle. *, AdminUser. name as AdminName, Membership. name as MembershipName, Membership. tel, Membership. cardId, Membership. address, Membership. source "). from ("Vehicle left join AdminUser on Vehicle. adminId = AdminUser. id left join Membership on Vehicle. membershipId = Membership. id "). orderBy ("Vehicle. time desc "). paging (pageInfo. pageI Ndex, pageInfo. PageSize); if (! String. isNullOrEmpty (Name) {select. andWhere ("Membership. name like @ Name "). parameter ("Name", string. format ("% {0} %", Name);} if (! String. isNullOrEmpty (Code) {select. andWhere ("Code = @ Code "). parameter ("Code", Code);} this. gridViewVehicle. dataSource = select. queryparts ();
Transaction usage:
// The transaction automatically rolls back the using (var context = FluentDataHelper. context (). useTransaction (true) {context. SQL ("update Membership set Name = @ 0 where Id = @ 1 "). parameters ("Li Si", 12 ). execute (); context. SQL ("update Product set Name = @ 0 where Id = @ 1 "). parameters ("Product does not exist in Database", 1 ). execute (); context. commit ();}
Well, it's almost easy to use. If you are interested, you can check out the source code. This is open source and the scalability is also very good.