This commit is contained in:
sunkaixuan
2017-05-29 01:30:23 +08:00
parent 8f7907d1ce
commit 2fa8f455a6
8 changed files with 69 additions and 66 deletions

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OrmTest.Demo
{
public class DbFirst
{
public static void Init()
{
}
}
}

View File

@@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace OrmTest.Demo namespace OrmTest.Demo
{ {
public class Delete public class Delete:DemoBase
{ {
public static void Init() public static void Init()
{ {
@@ -29,17 +29,5 @@ namespace OrmTest.Demo
//by expression //by expression
var t5 = db.Deleteable<Student>().Where(it => it.Id == 1).ExecuteCommand(); var t5 = db.Deleteable<Student>().Where(it => it.Id == 1).ExecuteCommand();
} }
public static SqlSugarClient GetInstance()
{
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
db.Ado.IsEnableLogEvent = true;
db.Ado.LogEventStarting = (sql, pars) =>
{
Console.WriteLine(sql + "\r\n" + db.RewritableMethods.SerializeObject(pars));
Console.WriteLine();
};
return db;
}
} }
} }

View File

@@ -0,0 +1,23 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OrmTest.Demo
{
public class DemoBase
{
public static SqlSugarClient GetInstance()
{
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
db.Ado.IsEnableLogEvent = true;
db.Ado.LogEventStarting = (sql, pars) =>
{
Console.WriteLine(sql + "\r\n" + db.RewritableMethods.SerializeObject(pars));
Console.WriteLine();
};
return db;
}
}
}

View File

@@ -8,13 +8,14 @@ using System.Threading.Tasks;
namespace OrmTest.Demo namespace OrmTest.Demo
{ {
public class Insert public class Insert:DemoBase
{
public static void Init()
{ {
public static void Init() {
var db = GetInstance(); var db = GetInstance();
db.IgnoreColumns.Add("TestId", "Student"); db.IgnoreColumns.Add("TestId", "Student");
var insertObj = new Student() { Name = "jack", CreateTime = Convert.ToDateTime("2010-1-1") ,SchoolId=1}; var insertObj = new Student() { Name = "jack", CreateTime = Convert.ToDateTime("2010-1-1"), SchoolId = 1 };
//Insert reutrn Insert Count //Insert reutrn Insert Count
var t2 = db.Insertable(insertObj).ExecuteCommand(); var t2 = db.Insertable(insertObj).ExecuteCommand();
@@ -24,7 +25,7 @@ namespace OrmTest.Demo
//Only insert Name //Only insert Name
var t4 = db.Insertable(insertObj).InsertColumns(it => new { it.Name,it.SchoolId }).ExecuteReutrnIdentity(); var t4 = db.Insertable(insertObj).InsertColumns(it => new { it.Name, it.SchoolId }).ExecuteReutrnIdentity();
//Ignore TestId //Ignore TestId
@@ -50,17 +51,5 @@ namespace OrmTest.Demo
} }
var s9 = db.Insertable(insertObjs.ToArray()).InsertColumns(it => new { it.Name }).ExecuteCommand(); var s9 = db.Insertable(insertObjs.ToArray()).InsertColumns(it => new { it.Name }).ExecuteCommand();
} }
public static SqlSugarClient GetInstance()
{
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
db.Ado.IsEnableLogEvent = true;
db.Ado.LogEventStarting = (sql, pars) =>
{
Console.WriteLine(sql + "\r\n" + db.RewritableMethods.SerializeObject(pars));
Console.WriteLine();
};
return db;
}
} }
} }

View File

@@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace OrmTest.Demo namespace OrmTest.Demo
{ {
public class Query public class Query:DemoBase
{ {
public static void Init() public static void Init()
@@ -273,18 +273,5 @@ namespace OrmTest.Demo
.Select("st.*").ToList(); .Select("st.*").ToList();
//SELECT st.* FROM [Student] st Left JOIN School sh ON sh.id=st.schoolid WHERE st.id>@id //SELECT st.* FROM [Student] st Left JOIN School sh ON sh.id=st.schoolid WHERE st.id>@id
} }
public static SqlSugarClient GetInstance()
{
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig{ ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
db.Ado.IsEnableLogEvent = true;
db.Ado.LogEventStarting = (sql, pars) =>
{
Console.WriteLine(sql + "\r\n" + db.RewritableMethods.SerializeObject(pars));
Console.WriteLine();
};
return db;
}
} }
} }

View File

@@ -8,9 +8,10 @@ using System.Threading.Tasks;
namespace OrmTest.Demo namespace OrmTest.Demo
{ {
public class Update public class Update : DemoBase
{
public static void Init()
{ {
public static void Init() {
var db = GetInstance(); var db = GetInstance();
var updateObj = new Student() { Id = 1, Name = "jack", SchoolId = 0, CreateTime = Convert.ToDateTime("2017-05-21 09:56:12.610") }; var updateObj = new Student() { Id = 1, Name = "jack", SchoolId = 0, CreateTime = Convert.ToDateTime("2017-05-21 09:56:12.610") };
var updateObjs = new List<Student>() { updateObj, new Student() { Id = 2, Name = "sun", SchoolId = 0 } }.ToArray(); var updateObjs = new List<Student>() { updateObj, new Student() { Id = 2, Name = "sun", SchoolId = 0 } }.ToArray();
@@ -19,7 +20,7 @@ namespace OrmTest.Demo
//update reutrn Update Count //update reutrn Update Count
var t1= db.Updateable(updateObj).ExecuteCommand(); var t1 = db.Updateable(updateObj).ExecuteCommand();
//Only update Name //Only update Name
var t3 = db.Updateable(updateObj).UpdateColumns(it => new { it.Name }).ExecuteCommand(); var t3 = db.Updateable(updateObj).UpdateColumns(it => new { it.Name }).ExecuteCommand();
@@ -47,23 +48,12 @@ namespace OrmTest.Demo
//Update By Expression Where By Expression //Update By Expression Where By Expression
var t10 = db.Updateable<Student>() var t10 = db.Updateable<Student>()
.UpdateColumns(it => new Student() { Name="a",CreateTime=DateTime.Now }) .UpdateColumns(it => new Student() { Name = "a", CreateTime = DateTime.Now })
.Where(it => it.Id == 11).ExecuteCommand(); .Where(it => it.Id == 11).ExecuteCommand();
//Rename //Rename
db.Updateable<School>().AS("Student").UpdateColumns(it => new School() { Name = "jack" }).Where(it => it.Id == 1).ExecuteCommand(); db.Updateable<School>().AS("Student").UpdateColumns(it => new School() { Name = "jack" }).Where(it => it.Id == 1).ExecuteCommand();
//Update Student set Name='jack' Where Id=1 //Update Student set Name='jack' Where Id=1
} }
public static SqlSugarClient GetInstance()
{
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
db.Ado.IsEnableLogEvent = true;
db.Ado.LogEventStarting = (sql, pars) =>
{
Console.WriteLine(sql + "\r\n" + db.RewritableMethods.SerializeObject(pars));
Console.WriteLine();
};
return db;
}
} }
} }

View File

@@ -48,7 +48,9 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Config.cs" /> <Compile Include="Config.cs" />
<Compile Include="Demo\DbFirst.cs" />
<Compile Include="Demo\Delete.cs" /> <Compile Include="Demo\Delete.cs" />
<Compile Include="Demo\DemoBase.cs" />
<Compile Include="Demo\Insert.cs" /> <Compile Include="Demo\Insert.cs" />
<Compile Include="Demo\Query.cs" /> <Compile Include="Demo\Query.cs" />
<Compile Include="Demo\Update.cs" /> <Compile Include="Demo\Update.cs" />

View File

@@ -119,6 +119,13 @@ namespace SqlSugar
public Dictionary<string, string> ToClassStringList(string nameSpace = "Models") public Dictionary<string, string> ToClassStringList(string nameSpace = "Models")
{ {
Dictionary<string, string> result = new Dictionary<string, string>(); Dictionary<string, string> result = new Dictionary<string, string>();
if (this.TableInfoList.IsValuable())
{
foreach (var tableInfo in this.TableInfoList)
{
}
}
this.Namespace = nameSpace; this.Namespace = nameSpace;
return result; return result;
} }
@@ -132,13 +139,15 @@ namespace SqlSugar
foreach (var item in classStringList) foreach (var item in classStringList)
{ {
string className = item.Key; string className = item.Key;
if (this.Context.MappingTables.IsValuable()) { if (this.Context.MappingTables.IsValuable())
var mappingInfo = this.Context.MappingTables.FirstOrDefault(it => it.DbTableName.Equals(item.Key,StringComparison.CurrentCultureIgnoreCase)); {
if (mappingInfo.IsValuable()) { var mappingInfo = this.Context.MappingTables.FirstOrDefault(it => it.DbTableName.Equals(item.Key, StringComparison.CurrentCultureIgnoreCase));
if (mappingInfo.IsValuable())
{
className = mappingInfo.EntityName; className = mappingInfo.EntityName;
} }
} }
FileHeper.CreateFile(directoryPath.TrimEnd('\\').TrimEnd('/') +string.Format("{0}\\.cs", className), item.Value, Encoding.UTF8); FileHeper.CreateFile(directoryPath.TrimEnd('\\').TrimEnd('/') + string.Format("{0}\\.cs", className), item.Value, Encoding.UTF8);
} }
} }
} }