动态建类功能优点:
1、多数据库支持更好,比如可以用来建表,CRUD等
2、支持类操作的功能,比如AOP等 ,特列类型IsJson等
var type = db.DynamicBuilder().CreateClass("table1", new SugarTable() { }) .CreateProperty("Id", typeof(int),new SugarColumn() { IsPrimaryKey = true, IsIdentity = true }) .CreateProperty("Name",typeof(string), new SugarColumn() { }) //.WithCache() 缓存起来根据表名和字段名组合的KEY .BuilderType(); //SugarColumn 属性说明看文档: 迁移 //创建表 db.CodeFirst.InitTables(type); //建表属性API看迁移 //根据字典转成类对象 var value= db.DynamicBuilder().CreateObjectByType(type,new Dictionary<string, object>() { { "Id", 1 }, { "Name", "jack" } }); db.InsertableByObject(value).ExecuteCommand(); db.UpdateableByObject(value).ExecuteCommand(); db.DeleteableByObject(value).ExecuteCommand(); db.StorageableByObject(value).ExecuteCommand();//插入或者更新 db.Queryable<object>().AsType(type).Filter(type).ToList();
var type = db.DynamicBuilder().CreateClass("table1", new SugarTable() { },null,null,new SplitTableAttribute(SplitType.Day)) .CreateProperty("Id", typeof(int), new SugarColumn() { IsPrimaryKey = true, IsIdentity = true }) .CreateProperty("Time", typeof(DateTime), new SugarColumn() { },true)//true表式分表字符 //.WithCache() 缓存起来根据表名和字段名组合的KEY .BuilderType(); db.InsertableByObject(value).SpiltTable().ExecuteCommand(); db.UpdateableByObject(value).SpiltTable().ExecuteCommand(); db.DeleteableByObject(value).SpiltTable().ExecuteCommand();
如果表已存在,并且不是CodeFirst建的请把主删了,否则可能因约束建表失败,是一种对原本的保护
var type = db.DynamicBuilder().CreateClass("table12", new SugarTable(){} ,typeof(Order)//继承Order类 ) .CreateProperty("yyy", typeof(string), new SugarColumn() { }) //.WithCache() 缓存起来根据表名和字段名组合的KEY .BuilderType(); db.CodeFirst.InitTables(type);
建表所有参数
public DynamicProperyBuilder CreateClass( string entityName, SugarTable table=null, Type baseType = null, //基类 Type[] interfaces = null,//接口 SplitTableAttribute splitTableAttribute=null)
2016 © donet5.comApache Licence 2.0