2 Star 0 Fork 0

大海202 / SqlSugar ORM 3.X

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

#Contact information

Email 610262374@qq.com

QQ Group 225982985

Blog http://www.cnblogs.com/sunkaixuan

#All versions

ASP.NET 4.0+ MSSQL https://github.com/sunkaixuan/SqlSugar

ASP.NET CORE MSSQL https://github.com/sunkaixuan/ASP_NET_CORE_ORM_SqlSugar

ASP.NET 4.0+ MYSQL https://github.com/sunkaixuan/MySqlSugar

ASP.NET 4.0+ Sqlite https://github.com/sunkaixuan/SqliteSugar

ASP.NET 4.0+ ORACLE https://github.com/sunkaixuan/OracleSugar

Instance SqlSugar object

using(var db = new SqlSugarClient(ConnectionString)){

	//use object
	var list=db.Queryable<T>().ToList();
	
}

Package instance

/// <summary>
/// SqlSugar
/// </summary>
public class SugarDao
{
	private SugarDao()
	{

	}
	public static string ConnectionString
	{
		get
		{
			string reval = "server=.;uid=sa;pwd=sasa;database=SqlSugarTest"; 
			return reval;
		}
	}
	public static SqlSugarClient GetInstance()
	{
		var db = new SqlSugarClient(ConnectionString);
		db.IsEnableLogEvent = true;//Enable log events
		db.LogEventStarting = (sql, par) => { Console.WriteLine(sql + " " + par+"\r\n"); };
		return db;
	}
}
Use SugarDao
using (var db = SugarDao.GetInstance())
{
	var list=db.Queryable<T>().ToList();
}

1.Select

1.1 queryable

//select all
var student = db.Queryable<Student>().ToList();
var studentDynamic = db.Queryable<Student>().ToDynamic();
var studentJson = db.Queryable<Student>().ToJson();


//select single
var single = db.Queryable<Student>().Single(c => c.id == 1);
//select single by primarykey
var singleByPk = db.Queryable<Student>().InSingle(1);
//select single or default
var singleOrDefault = db.Queryable<Student>().SingleOrDefault(c => c.id == 11111111);
//select single or default
var single2 = db.Queryable<Student>().Where(c => c.id == 1).SingleOrDefault();

//select first
var first = db.Queryable<Student>().Where(c => c.id == 1).First();
var first2 = db.Queryable<Student>().Where(c => c.id == 1).FirstOrDefault();

//between 11 and 20
var page1 = db.Queryable<Student>().Where(c => c.id > 10).OrderBy(it => it.id).Skip(10).Take(10).ToList();

//between 11 and 20 
var page2 = db.Queryable<Student>().Where(c => c.id > 10).OrderBy(it => it.id).ToPageList(2, 10);

//get count
var count = db.Queryable<Student>().Where(c => c.id > 10).Count();

//skip 2
var skip = db.Queryable<Student>().Where(c => c.id > 10).OrderBy(it => it.id).Skip(2).ToList();

//take 2
var take = db.Queryable<Student>().Where(c => c.id > 10).OrderBy(it => it.id).Take(2).ToList();

//Not like 
string conval = "a";
var notLike = db.Queryable<Student>().Where(c => !c.name.Contains(conval.ToString())).ToList();

//Like
conval = "三";
var like = db.Queryable<Student>().Where(c => c.name.Contains(conval)).ToList();

//where sql string
var student12 = db.Queryable<Student>().Where(c => "a" == "a").Where("id>@id", new { id = 1 }).ToList();
var student13 = db.Queryable<Student>().Where(c => "a" == "a").Where("id>100 and id in( select 1)").ToList();


//is any
bool isAny100 = db.Queryable<Student>().Any(c => c.id == 100);
bool isAny1 = db.Queryable<Student>().Any(c => c.id == 1);


//get max id
object maxId = db.Queryable<Student>().Max(it => it.id);
int maxId1 = db.Queryable<Student>().Max(it => it.id).ObjToInt();
int maxId2 = db.Queryable<Student>().Max<int>("id"); 

//get min id
int minId1 = db.Queryable<Student>().Where(c => c.id > 0).Min(it => it.id).ObjToInt();
int minId2 = db.Queryable<Student>().Where(c => c.id > 0).Min<int>("id");


//order By 
var orderList = db.Queryable<Student>().OrderBy("id desc,name asc").ToList();
//order by 
var order2List = db.Queryable<Student>().OrderBy(it => it.name).OrderBy(it => it.id, OrderByType.desc).ToList(); // order by name as ,order by id desc

//In
var intArray = new[] { "5", "2", "3" };
var intList = intArray.ToList();
var listnew = db.Queryable<Student>().Where(it => intArray.Contains(it.name)).ToList();
var list0 = db.Queryable<Student>().In(it => it.id, 1, 2, 3).ToList();
var list1 = db.Queryable<Student>().In(it => it.id, intArray).ToList();
var list2 = db.Queryable<Student>().In("id", intArray).ToList();
var list3 = db.Queryable<Student>().In(it => it.id, intList).ToList();
var list4 = db.Queryable<Student>().In("id", intList).ToList();
var list6 = db.Queryable<Student>().In(intList).ToList(); //in primary fileds

//group by
var list7 = db.Queryable<Student>().Where(c => c.id < 20).GroupBy(it => it.sex).Select("sex,count(*) Count").ToDynamic();
var list8 = db.Queryable<Student>().Where(c => c.id < 20).GroupBy(it => it.sex).GroupBy(it => it.id).Select("id,sex,count(*) Count").ToDynamic();
List<StudentGroup> list9 = db.Queryable<Student>().Where(c => c.id < 20).GroupBy(it => it.sex).Select<StudentGroup>("Sex,count(*) Count").ToList();
List<StudentGroup> list10 = db.Queryable<Student>().Where(c => c.id < 20).GroupBy("sex").Select<StudentGroup>("Sex,count(*) Count").ToList();
//SELECT Sex,Count=count(*)  FROM Student  WHERE 1=1  AND  (id < 20)    GROUP BY Sex --生成结果



//join
var jList = db.Queryable<Student>()
	.JoinTable<School>((s1, s2) => s1.sch_id == s2.id) //默认left join
	.Where<School>((s1, s2) => s1.id == 1)
	.Select("s1.*,s2.name as schName")
	.ToDynamic();

/*join sql
						 SELECT s1.*,s2.name as schName 
						 FROM [Student]  s1 
						 LEFT JOIN [School]  s2 ON  s1.sch_id  = s2.id 
						 WHERE  s1.id  = 1 */

//join and page
var jList2 = db.Queryable<Student>()
	.JoinTable<School>((s1, s2) => s1.sch_id == s2.id) //默认left join
	//left inner join 
	//.JoinTable<School>((s1, s2) => s1.sch_id == s2.id ,JoinType.INNER)
	.Where<School>((s1, s2) => s1.id > 1)
	.OrderBy(s1 => s1.name)
	.Skip(10)
	.Take(20)
	.Select("s1.*,s2.name as schName")
	.ToDynamic();

//join three tables
var jList3 = db.Queryable<Student>()
	.JoinTable<School>((s1, s2) => s1.sch_id == s2.id) // left join  School s2  on s1.id=s2.id
	.JoinTable<School>((s1, s3) => s1.sch_id == s3.id) // left join  School s3  on s1.id=s3.id
	.Where<School>((s1, s2) => s1.id > 1)  // where s1.id>1
	.Where(s1 => s1.id > 0)
	.OrderBy<School>((s1, s2) => s1.id) 
	.Skip(10)
	.Take(20)
	.Select("s1.*,s2.name as schName,s3.name as schName2")//select string
	.ToDynamic();


//join five
List<V_Student> jList4 =
	db.Queryable<Student>()
	.JoinTable<School>((s1, s2) => s1.sch_id == s2.id) // left join  School s2  on s1.id=s2.id
	.JoinTable<School, Area>((s1, s2, a1) => a1.id == s2.AreaId)// left join  Area a1  on a1.id=s2.AreaId  
		.JoinTable<Area, School>((s1, a1, s3) => a1.id == s3.AreaId)// left join  School s3  on a1.id=s3.AreaId  
			.JoinTable<School>((s1, s4) => s1.sch_id == s4.id) // left join  School s2  on s1.id=s4.id
			.Select<School, Area, V_Student>((s1, s2, a1) => new V_Student { id = s1.id, name = s1.name, SchoolName = s2.name, AreaName = a1.name }).ToList();

//join five sql
//SELECT id = s1.id, name = s1.name, SchoolName = s2.name, AreaName = a1.name  
//FROM [Student]   s1 
//LEFT JOIN School  s2 ON  ( s1.sch_id  = s2.id )    
//LEFT JOIN Area  a1 ON  ( a1.id  = s2.AreaId )     
//LEFT JOIN School  s3 ON  ( a1.id  = s3.AreaId )    
//LEFT JOIN School  s4 ON  ( s1.sch_id  = s4.id )    
//WHERE 1=1    


//Join child
var childQuery = db.Queryable<Area>().Where("id=@id").Select(it => new { id = it.id }).ToSql();//create child SQL
string childTableName =SqlSugarTool.PackagingSQL(childQuery.Key);//package sql
var queryable = db.Queryable<Student>()
	.JoinTable<School>((s1, s2) => s1.sch_id == s2.id)  //LEFT JOIN School  s2 ON  ( s1.sch_id  = s2.id )  
	.JoinTable(childTableName, "a1", "a1.id=s2.areaid", new { id = 1 }, JoinType.INNER) //INNER JOIN (SELECT *  FROM [Area]   WHERE 1=1  AND id=@id   ) a1 ON a1.id=s2.areaid
	.OrderBy(s1 => s1.id);

var list = queryable.Select<School, Area, V_Student>((s1, s2, a1) => new V_Student { id = s1.id, name = s1.name, SchoolName = s2.name, AreaName = a1.name })
	.ToPageList(0, 200);
var count2 = queryable.Count();


//append queryable
var queryable2 = db.Queryable<Student>().Where(it => true);
if (maxId.ObjToInt() == 1)
{
	queryable2.Where(it => it.id == 1);
}
else
{
	queryable2.Where(it => it.id == 2);
}
var listJoin = queryable2.ToList();


//queryable SqlSugarClient 
var par = new Queryable<Student>().Where(it => it.id == 1);//声名没有connection对象的Queryable
par.DB = db;
var listPar = par.ToList();


//get sql和 pars
var id = 1;
var sqlAndPars = db.Queryable<Student>().Where(it => it.id == id).OrderBy(it => it.id).ToSql();



//express functions
var par1 = "2015-1-1"; var par2 = "   I have a trim  ";
var r1 = db.Queryable<Student>().Where(it => it.name == par1.ObjToString()).ToList(); //ObjToString if null return ""
var r2 = db.Queryable<InsertTest>().Where(it => it.d1 == par1.ObjToDate()).ToList();
var r3 = db.Queryable<InsertTest>().Where(it => it.id == 1.ObjToInt()).ToList();//ObjToInt if null return 0
var r4 = db.Queryable<InsertTest>().Where(it => it.id == 2.ObjToDecimal()).ToList();
var r5 = db.Queryable<InsertTest>().Where(it => it.id == 3.ObjToMoney()).ToList();
var r6 = db.Queryable<InsertTest>().Where(it => it.v1 == par2.Trim()).ToList();
var convert1 = db.Queryable<Student>().Where(c => c.name == "a".ToString()).ToList();
var convert2 = db.Queryable<Student>().Where(c => c.id == Convert.ToInt32("1")).ToList();
var convert3 = db.Queryable<Student>().Where(c => c.name == par2.ToLower()).ToList();
var convert4 = db.Queryable<Student>().Where(c => c.name == par2.ToUpper()).ToList();
var convert5= db.Queryable<Student>().Where(c => DateTime.Now > Convert.ToDateTime("2015-1-1")).ToList();
var c1 = db.Queryable<Student>().Where(c => c.name.Contains("a")).ToList();
var c2 = db.Queryable<Student>().Where(c => c.name.StartsWith("a")).ToList();
var c3 = db.Queryable<Student>().Where(c => c.name.EndsWith("a")).ToList();
var c4 = db.Queryable<Student>().Where(c => !string.IsNullOrEmpty(c.name)).ToList();
var c5 = db.Queryable<Student>().Where(c => c.name.Equals("小杰")).ToList();
var c6 = db.Queryable<Student>().Where(c => c.name.Length > 4).ToList();
var time = db.Queryable<InsertTest>().Where(c => c.d1>DateTime.Now.AddDays(1)).ToList();
var time2 = db.Queryable<InsertTest>().Where(c => c.d1 > DateTime.Now.AddYears(1)).ToList();
var time3 = db.Queryable<InsertTest>().Where(c => c.d1 > DateTime.Now.AddMonths(1)).ToList();
var intList = intArray.ToList();
var list0 = db.Queryable<Student>().In(it => it.id, 1,2,3).ToList();
var list1 = db.Queryable<Student>().In(it=>it.id, intArray).ToList();
var list2 = db.Queryable<Student>().In("id", intArray).ToList();
var list3 = db.Queryable<Student>().In(it => it.id, intList).ToList();
var list4 = db.Queryable<Student>().In("id", intList).ToList();

//group by
var list7 = db.Queryable<Student>().Where(c => c.id < 20).GroupBy(it => it.sex).Select("sex,Count=count(*)").ToDynamic();
var list8 = db.Queryable<Student>().Where(c => c.id < 20).GroupBy(it => it.sex).GroupBy(it=>it.id).Select("id,sex,Count=count(*)").ToDynamic();
List<SexTotal> list5 = db.Queryable<Student>().Where(c => c.id < 20).GroupBy(it => it.sex).Select<Student, SexTotal>("Sex,Count=count(*)").ToList();
List<SexTotal> list6 = db.Queryable<Student>().Where(c => c.id < 20).GroupBy("sex").Select<Student, SexTotal>    ("Sex,Count=count(*)").ToList();
//SELECT Sex,Count=count(*)  FROM Student  WHERE 1=1  AND  (id < 20)    GROUP BY Sex 


//join
var jList = db.Queryable<Student>()
	.JoinTable<Student, School>((s1, s2) => s1.sch_id == s2.id) //detault left join
		.Where<Student, School>((s1, s2) => s1.id == 1)
			.Select("s1.*,s2.name as schName")
			.ToDynamic();

//join by page
var jList2 = db.Queryable<Student>()
	.JoinTable<Student, School>((s1, s2) => s1.sch_id == s2.id) //default left join
		//inner join
		//.JoinTable<Student, School>((s1, s2) => s1.sch_id == s2.id  ,JoinType.INNER)
		.Where<Student, School>((s1, s2) => s1.id > 1)
			.OrderBy<Student, School>((s1, s2) => s1.name)
				.Skip(10)
				.Take(20)
				.Select("s1.*,s2.name as schName")
				.ToDynamic();

//join select new 
var jList3 = db.Queryable<Student>()
	.JoinTable<Student, School>((s1, s2) => s1.sch_id == s2.id) // left join  School s2  on s1.id=s2.id
		.Where<Student, School>((s1, s2) => s1.id > 1)  // where s1.id>1
			.OrderBy<Student, School>((s1, s2) => s1.id) //order by s1.id no one  ordder   .oderBy().orderby  
				.Skip(1)
				.Take(2)
				.Select<Student, School, classNew>((s1, s2) => new classNew() { newid = s1.id, newname = s2.name, xx_name = s1.name }) 
					.ToList();

1.2 SqlQuery

//to list
List<Student> list1 = db.SqlQuery<Student>("select * from Student");
//to list with par
List<Student> list2 = db.SqlQuery<Student>("select * from Student where id=@id", new { id = 1 });
//to dynamic
dynamic list3 = db.SqlQueryDynamic("select * from student");
//to json
string list4 = db.SqlQueryJson("select * from student");
//get int
var list5 = db.SqlQuery<int>("select top 1 id from Student").SingleOrDefault();
//get dictionary
Dictionary<string, string> list6 = db.SqlQuery<KeyValuePair<string, string>>("select id,name from Student").ToDictionary(it => it.Key, it => it.Value);
//get List<string[]>
var list7 = db.SqlQuery<string[]>("select top 1 id,name from Student").SingleOrDefault();
//get sp result
var spResult = db.SqlQuery<School>("exec sp_school @p1,@p2", new { p1 = 1, p2 = 2 });

//get sp and output 
var pars = SqlSugarTool.GetParameters(new { p1 = 1,p2=0 }); //dynmaic to SqlParameter
db.IsClearParameters = false;//close clear parametrs
pars[1].Direction = ParameterDirection.Output; //set  output
var spResult2 = db.SqlQuery<School>("exec sp_school @p1,@p2 output", pars);
db.IsClearParameters = true;//open  clear parameters
var outPutValue = pars[1].Value;//get output @p2 value

//sp 
var pars2 = SqlSugarTool.GetParameters(new { p1 = 1, p2 = 0 }); 
db.CommandType = CommandType.StoredProcedure;
var spResult3 = db.SqlQuery<School>("sp_school", pars2);
db.CommandType = CommandType.Text;


//get first row first column
string v1 = db.GetString("select '张三' as name");
int v2 = db.GetInt("select 1 as name");
double v3 = db.GetDouble("select 1 as name");
decimal v4 = db.GetDecimal("select 1 as name");
//....
1.3 Sqlable
//join
List<School> dataList = db.Sqlable()
	.From("school", "s")
	.Join("student", "st", "st.id", "s.id", JoinType.INNER)
	.Join("student", "st2", "st2.id", "st.id", JoinType.LEFT)
	.Where("s.id>100 and s.id<@id")
	.Where("1=1")
	.OrderBy("id")
	.SelectToList<School/*new model*/>("st.*", new { id = 1 });

//join page
List<School> dataPageList = db.Sqlable()
	.From("school", "s")
	.Join("student", "st", "st.id", "s.id", JoinType.INNER)
	.Join("student", "st2", "st2.id", "st.id", JoinType.LEFT)
	.Where("s.id>100 and s.id<100")
	.SelectToPageList<School>("st.*", "s.id", 1, 10);

//page where
List<School> dataPageList2 = db.Sqlable()
	.From("school", "s")
	.Join("student", "st", "st.id", "s.id", JoinType.INNER)
	.Join("student", "st2", "st2.id", "st.id", JoinType.LEFT)
	.Where("s.id>100 and s.id<100 and s.id in (select 1 )" )
	.SelectToPageList<School>("st.*", "s.id", 1, 10);



//-------- Dynmaic OR Json-----//

//join
var list1 = db.Sqlable().From("student", "s").Join("school", "l", "s.sch_id", "l.id and l.id=@id", JoinType.INNER).SelectToDynamic("*", new { id = 1 });
var list2 = db.Sqlable().From("student", "s").Join("school", "l", "s.sch_id", "l.id and l.id=@id", JoinType.INNER).SelectToJson("*", new { id = 1 });
var list3 = db.Sqlable().From("student", "s").Join("school", "l", "s.sch_id", "l.id and l.id=@id", JoinType.INNER).SelectToDataTable("*", new { id = 1 });

//page
var list4 = db.Sqlable().From("student", "s").Join("school", "l", "s.sch_id", "l.id and l.id=@id", JoinType.INNER).SelectToPageDynamic("s.*", "l.id", 1, 10, new { id = 1 });
var list5 = db.Sqlable().From("student", "s").Join("school", "l", "s.sch_id", "l.id and l.id=@id", JoinType.INNER).SelectToPageTable("s.*", "l.id", 1, 10, new { id = 1 });
var list6 = db.Sqlable().From("student", "s").Join("school", "l", "s.sch_id", "l.id and l.id=@id", JoinType.INNER).SelectToPageDynamic("s.*", "l.id", 1, 10, new { id = 1 });


//--------append sqlable-----//
Sqlable sable = db.Sqlable().From<Student>("s").Join<School>("l", "s.sch_id", "l.id", JoinType.INNER);
string name = "a";
int id = 1;
if (!string.IsNullOrEmpty(name))
{
	sable = sable.Where("s.name=@name");
}
if (!string.IsNullOrEmpty(name))
{
	sable = sable.Where("s.id=@id or s.id=100");
}
if (id > 0)
{
	sable = sable.Where("l.id in (select top 10 id from school)");
}
var pars = new { id = id, name = name };
int pageCount = sable.Count(pars);
var list7 = sable.SelectToPageList<Student>("s.*", "l.id desc", 1, 20, pars);

2.Insert

//insert item
db.Insert(GetInsertItem());  

//insert list
db.InsertRange(GetInsertList()); 

//insert list (so fast)
db.SqlBulkCopy(GetInsertList()); 


//setting disable insert columns
db.DisableInsertColumns = new string[] { "sex" };
Student s = new Student()
{
	name = "mr" + new Random().Next(1, int.MaxValue),
	sex = "gril"
};

var id = db.Insert(s); // insert  with no 【sex】

3.Update

//update specified column
db.Update<School>(new { name = "蓝翔14" }, it => it.id == 14); //only update name
db.Update<School, int>(new { name = "蓝翔11 23 12", areaId = 2 }, 11, 23, 12);
db.Update<School, string>(new { name = "蓝翔2" }, new string[] { "11", "21" });
db.Update<School>(new { name = "蓝翔2" }, it => it.id == 100);
var array=new int[]{1,2,3};
db.Update<School>(new { name = "蓝翔2" }, it => array.Contains(it.id));// id in 1,2,3

//update list  by enity primary key
var updateResult = db.UpdateRange(GetUpdateList());a

//update list  by enity primary key (so fast)
var updateResult2 = db.SqlBulkReplace(GetUpdateList2());

//update by dictionary
var dic = new Dictionary<string, string>();
dic.Add("name", "第十三条");
dic.Add("areaId", "1");
db.Update<School, int>(dic, 13);


//update  by  entity
db.Update(new School { id = 16, name = "蓝翔16", AreaId = 1 });
db.Update<School>(new School { id = 12, name = "蓝翔12", AreaId = 2 }, it => it.id == 18);
db.Update<School>(new School() { id = 11, name = "青鸟11" });

//settig disable Update Columns
db.DisableUpdateColumns = new string[] { "CreateTime" };//CreateTime no update

TestUpdateColumns updObj = new TestUpdateColumns()
{
	VGUID = Guid.Parse("542b5a27-6984-47c7-a8ee-359e483c8470"),
	Name = "xx",
	Name2 = "xx2",
	IdentityField = 0,
	CreateTime = null
};

db.Update(updObj);

4.Delete

//delete by primary key
db.Delete<School, int>(10);

//delete by exp
db.Delete<School>(it => it.id > 100);//support it=>array.contains(it.id)

//delete by primary key values
db.Delete<School, string>(new string[] { "100", "101", "102" });

//delete by field values
db.Delete<School, string>(it => it.name, new string[] { "" });
db.Delete<School, int>(it => it.id, new int[] { 20, 22 });


//delete by entity
db.Delete(new School() { id = 200 });

//delete by sql where  string
db.Delete<School>("id=@id", new { id = 100 });

//false delete
//db.FalseDelete<school>("is_del", 100);
//sql: update school set is_del=1 where id in(100)
//db.FalseDelete<school>("is_del", it=>it.id==100);

5.Tran

using (SqlSugarClient db = SugarDao.GetInstance())
{
	db.IsNoLock = true; 
	db.CommandTimeOut = 30000; 
	try
	{
		db.BeginTran();
		//db.BeginTran(IsolationLevel.ReadCommitted);+3

		db.CommitTran();
	}
	catch (Exception)
	{
		db.RollbackTran();
		throw;
	}
}

6.Rename tables

public class MappingTable : IDemos
{

	public void Init()
	{
		Console.WriteLine("启动MappingTable.Init");

		//单个设置
		using (var db = SugarDao.GetInstance())
		{
			var list = db.Queryable<V_Student>("Student").ToList();//查询的是 select * from student 而我的实体名称为V_Student
		}


		//全局设置
		using (var db = SugarFactory.GetInstance())
		{
			var list = db.Queryable<V_Student>().ToList();//查询的是 select * from student 而我的实体名称为V_Student
		}
	}


	/// <summary>
	/// 全局配置类
	/// </summary>
	public class SugarConfigs
	{
		//key类名 value表名
		public static List<KeyValue> MpList = new List<KeyValue>(){
			new KeyValue(){ Key="FormAttr", Value="Flow_FormAttr"},
				new KeyValue(){ Key="Student3", Value="Student"},
				new KeyValue(){ Key="V_Student", Value="Student"}
		};
	}

	/// <summary>
	/// SqlSugar实例工厂
	/// </summary>
	public class SugarFactory
	{

		//禁止实例化
		private SugarFactory()
		{

		}
		public static SqlSugarClient GetInstance()
		{
			string connection = SugarDao.ConnectionString; //这里可以动态根据cookies或session实现多库切换
			var db = new SqlSugarClient(connection);

			db.SetMappingTables(SugarConfigs.MpList);//设置关联表 (引用地址赋值,每次赋值都只是存储一个内存地址)



			//批量设置别名表
			//db.ClassGenerating.ForeachTables(db, tableName =>
			//{
			//    db.AddMappingTable(new KeyValue() { Key = tableName.Replace("bbs.",""), Value =  tableName }); //key实体名,value表名
			//});


			return db;
		}
	}
}

7.Rename Columns

//别名列的功能
public class MappingColumns : IDemos
{

	public void Init()
	{
		Console.WriteLine("启动MappingColumns.Init");

		//全局设置
		using (var db = SugarFactory.GetInstance())
		{
			var list = db.Queryable<Student>().Where(it=>it.classId==1).ToList();
		}
	}

	public class Student
	{

		//id
		public int classId { get; set; }

		//name
		public string className { get; set; }

		//sch_id
		public int classSchoolId { get; set; }

		public int isOk { get; set; }
	}

	/// <summary>
	/// 全局配置别名列(不区分表)
	/// </summary>
	public class SugarConfigs
	{
		//key实体字段名 value表字段名 ,KEY唯一否则异常
		public static List<KeyValue> MpList = new List<KeyValue>(){
			new KeyValue(){ Key="classId", Value="id"},
				new KeyValue(){ Key="className", Value="name"},
				new KeyValue(){ Key="classSchoolId", Value="sch_id"}
		};
	}

	/// <summary>
	/// SqlSugar实例工厂
	/// </summary>
	public class SugarFactory
	{

		//禁止实例化
		private SugarFactory()
		{

		}
		public static SqlSugarClient GetInstance()
		{
			string connection = SugarDao.ConnectionString; //这里可以动态根据cookies或session实现多库切换
			var db = new SqlSugarClient(connection);
			//注意:只有启动属性映射才可以使用SetMappingColumns
			db.IsEnableAttributeMapping = true;
			db.SetMappingColumns(SugarConfigs.MpList);//设置关联列 (引用地址赋值,每次赋值都只是存储一个内存地址)
			return db;
		}
	}
}

#8.Ignore error columns

using (var db = SugarDao.GetInstance())
{
	db.IsIgnoreErrorColumns = true;
}

#9.Create class files

db.ClassGenerating.CreateClassFiles(db, ("e:/TestModels"), "Models");

More http://www.cnblogs.com/sunkaixuan/p/5911334.html

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: You must give any other recipients of the Work or Derivative Works a copy of this License; and You must cause any modified files to carry prominent notices stating that You changed the files; and You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2016 sunkaixuan Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

众多国内ORM中的一匹黑马,已经在项目中投入使用。并且拥有一流的性能,强劲的语法。多库并行计算等出色的功能。 展开 收起
C#
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C#
1
https://gitee.com/dahaihai/SqlSugar-ORM-3.X.git
git@gitee.com:dahaihai/SqlSugar-ORM-3.X.git
dahaihai
SqlSugar-ORM-3.X
SqlSugar ORM 3.X
master

搜索帮助