是一个全新的版本, 几乎全部重写了以前的代码,设计的结构也简化很多,基于.net4,使用lambda表达式代替反射,去除大量过时的功能(如DataTable和DataSet的相关功能等),去除自定义表达式部分,通过解析lambda表达式实现自定义条件查询。 使用.net自带Configuration功能,只要web.config或app.config,不需要额外的配置文件,方便配置使用;去除xml mapping方式,支持DLinq属性。支持多种数据库(已测试sql server, mysql, sqlite及oracle), 支持(ubuntu 11.10 + mono 2.10.5 + mysql 5.1.58测试通过)。
代码示例:
初启化,通过配置文件或connection string构造DataContext:
var dc = new DataContext("RaisingStudio.Data.TestProject.Properties.Settings.DefaultConnectionString"); 或 var dc = new DataContext("server=localhost;User Id=root;password=root;database=test;");
lambda表达式查询:
var q = this.dc.Query- (i => i.Name == "test" && i.Status == "ok");
或linq查询:
var q = from s in dc.GetQuery() where s.SuppId > 0 && s.Name == "for Test3" select s;
增删改:
this.dc.Insert(new Product { ProductId = Guid.NewGuid().ToString(), CategoryId = categoryId, Name = "for Test4" }); this.dc.Delete (suppId); this.dc.Update (product); this.dc.Save (category);
通过主键获取单个实例:
var product = this.dc.GetEntity(productId);
支持排序和分页:
List- items = this.dc.Query
- (ExpressionExtension.Empty
- ().Take(3)).ToList(); var q = (from i in this.dc.GetQuery
- () select i).Skip(1).Take(2); int count = this.dc.Query
- (ExpressionExtension.Empty
- ().Skip(1)).Count();
部分列:
this.dc.Save- (item, new string[] { "UnitCost" }); this.dc.Insert
- (item, new Expression
- >[] { i => i.ItemId, i => i.Name, i => i.ListPrice, i => i.ProductId, i => i.Status });
无实例类查询:
Command command = new Command("SELECT * FROM Product WHERE CategoryId = @p1"); command.AddParameter("@p1", categoryId); var q = this.dc.Query(command); int count = 0; foreach (dynamic p in q) { Assert.IsNotNull(p); Assert.AreEqual(p.CategoryId, categoryId); count++; }
事务处理:
this.dc.BeginTransaction(); try { int result = this.dc.Insert(new Supplier { Name = "s for Test6", Status = "done", City = "dalian" }); int suppId = Convert.ToInt32(this.dc.GetIdentity ()); result = this.dc.Delete (suppId); Category category = new Category { CategoryId = Guid.NewGuid().ToString(), Name = "c for Test6", Descn = "d for Test6" }; Product product = new Product { ProductId = Guid.NewGuid().ToString(), Name = "p for Test6", Descn = "d for Test6", CategoryId = category.CategoryId }; result = this.dc.Insert (category); result = this.dc.Insert (product); this.dc.CommitTransaction(); } catch (Exception ex) { this.dc.RollbackTransaction(); throw ex; }
配置文件示例:
下载地址:
相关阅读:
(实体生成工具使用RazorEngine,模版可轻松实制)