mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-07-15 05:13:27 +08:00
Update Demo
This commit is contained in:
parent
2f38dc4750
commit
9b789010bb
7
Src/Asp.Net/SugarCodeGeneration/Codes/BLLParameter.cs
Normal file
7
Src/Asp.Net/SugarCodeGeneration/Codes/BLLParameter.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace SugarCodeGeneration
|
||||
{
|
||||
public class BLLParameter
|
||||
{
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
12
Src/Asp.Net/SugarCodeGeneration/Codes/DbContextParameter.cs
Normal file
12
Src/Asp.Net/SugarCodeGeneration/Codes/DbContextParameter.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using System.Collections.Generic;
|
||||
using SqlSugar;
|
||||
|
||||
namespace SugarCodeGeneration
|
||||
{
|
||||
public class DbContextParameter
|
||||
{
|
||||
public string ConnectionString { get; set; }
|
||||
public DbType DbType { get; set; }
|
||||
public List<string> Tables { get; set; }
|
||||
}
|
||||
}
|
70
Src/Asp.Net/SugarCodeGeneration/Codes/FileHelper.cs
Normal file
70
Src/Asp.Net/SugarCodeGeneration/Codes/FileHelper.cs
Normal file
@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SugarCodeGeneration.Codes
|
||||
{
|
||||
internal class FileHelper
|
||||
{
|
||||
public static void CreateFile(string filePath, string text, Encoding encoding)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (IsExistFile(filePath))
|
||||
{
|
||||
DeleteFile(filePath);
|
||||
}
|
||||
if (!IsExistFile(filePath))
|
||||
{
|
||||
string directoryPath = GetDirectoryFromFilePath(filePath);
|
||||
CreateDirectory(directoryPath);
|
||||
|
||||
//Create File
|
||||
FileInfo file = new FileInfo(filePath);
|
||||
using (FileStream stream = file.Create())
|
||||
{
|
||||
using (StreamWriter writer = new StreamWriter(stream, encoding))
|
||||
{
|
||||
writer.Write(text);
|
||||
writer.Flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
public static bool IsExistDirectory(string directoryPath)
|
||||
{
|
||||
return Directory.Exists(directoryPath);
|
||||
}
|
||||
public static void CreateDirectory(string directoryPath)
|
||||
{
|
||||
if (!IsExistDirectory(directoryPath))
|
||||
{
|
||||
Directory.CreateDirectory(directoryPath);
|
||||
}
|
||||
}
|
||||
public static void DeleteFile(string filePath)
|
||||
{
|
||||
if (IsExistFile(filePath))
|
||||
{
|
||||
File.Delete(filePath);
|
||||
}
|
||||
}
|
||||
public static string GetDirectoryFromFilePath(string filePath)
|
||||
{
|
||||
FileInfo file = new FileInfo(filePath);
|
||||
DirectoryInfo directory = file.Directory;
|
||||
return directory.FullName;
|
||||
}
|
||||
public static bool IsExistFile(string filePath)
|
||||
{
|
||||
return File.Exists(filePath);
|
||||
}
|
||||
}
|
||||
}
|
@ -4,9 +4,14 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml.Linq;
|
||||
using RazorEngine;
|
||||
using RazorEngine.Templating;
|
||||
|
||||
namespace SugarCodeGeneration.Codes
|
||||
{
|
||||
/// <summary>
|
||||
/// 生成所需要的代码
|
||||
/// </summary>
|
||||
public class Methods
|
||||
{
|
||||
public static string GetCurrentProjectPath
|
||||
@ -31,11 +36,14 @@ namespace SugarCodeGeneration.Codes
|
||||
public static void AddCsproj(string classPath, string projectName)
|
||||
{
|
||||
var classDirectory = Methods.GetSlnPath + "\\" +projectName+"\\"+ classPath.TrimStart('\\');
|
||||
if (FileHelper.IsExistDirectory(classDirectory) == false) {
|
||||
FileHelper.CreateDirectory(classDirectory);
|
||||
}
|
||||
var files = Directory.GetFiles(classDirectory).ToList().Select(it=>classPath+"\\"+Path.GetFileName(it));
|
||||
var xmlPath = GetSlnPath + @"\" + projectName + @"\SugarCodeGeneration.csproj";
|
||||
|
||||
var xml = File.ReadAllText(xmlPath,Encoding.UTF8);
|
||||
var firstLine = System.IO.File.ReadLines(xmlPath, Encoding.UTF8).First();
|
||||
var xml = File.ReadAllText(xmlPath, System.Text.Encoding.UTF8);
|
||||
var firstLine = System.IO.File.ReadLines(xmlPath, System.Text.Encoding.UTF8).First();
|
||||
var newXml = xml.Replace(firstLine, "").TrimStart('\r').TrimStart('\n');
|
||||
XDocument xe = XDocument.Load(xmlPath);
|
||||
var itemGroup=xe.Root.Elements().Where(it=>it.Name.LocalName== "ItemGroup"&&it.Elements().Any(y=>y.Name.LocalName== "Compile")).First();
|
||||
@ -52,5 +60,28 @@ namespace SugarCodeGeneration.Codes
|
||||
xe = XDocument.Parse(newXml);
|
||||
xe.Save(xmlPath);
|
||||
}
|
||||
|
||||
public static void CreateBLL(string templatePath, string savePath,List<string> tables)
|
||||
{
|
||||
|
||||
string template = System.IO.File.ReadAllText(templatePath); //从文件中读出模板内容
|
||||
string templateKey = "bll"; //取个名字
|
||||
foreach (var item in tables)
|
||||
{
|
||||
BLLParameter model = new BLLParameter()
|
||||
{
|
||||
Name=item
|
||||
};
|
||||
var result = Engine.Razor.RunCompile(template, templateKey, model.GetType(), model);
|
||||
FileHelper.CreateFile(savePath+"\\"+item+ "Manager.cs", result, System.Text.Encoding.UTF8);
|
||||
}
|
||||
}
|
||||
|
||||
public static void CreateDbContext(string templatePath, string savePath, object model) {
|
||||
string template = System.IO.File.ReadAllText(templatePath); //从文件中读出模板内容
|
||||
string templateKey ="dbcontext"; //取个名字
|
||||
var result =Engine.Razor.RunCompile(template, templateKey, model.GetType(), model);
|
||||
FileHelper.CreateFile(savePath, result, System.Text.Encoding.UTF8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,31 +0,0 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace MyTest
|
||||
{
|
||||
///<summary>
|
||||
///
|
||||
///</summary>
|
||||
public partial class School
|
||||
{
|
||||
public School(){
|
||||
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// Desc:
|
||||
/// Default:
|
||||
/// Nullable:False
|
||||
/// </summary>
|
||||
public int Id {get;set;}
|
||||
|
||||
/// <summary>
|
||||
/// Desc:
|
||||
/// Default:
|
||||
/// Nullable:True
|
||||
/// </summary>
|
||||
public string Name {get;set;}
|
||||
|
||||
}
|
||||
}
|
@ -1,66 +0,0 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace MyTest
|
||||
{
|
||||
///<summary>
|
||||
///
|
||||
///</summary>
|
||||
public partial class Student
|
||||
{
|
||||
public Student(){
|
||||
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// Desc:
|
||||
/// Default:
|
||||
/// Nullable:False
|
||||
/// </summary>
|
||||
public int Id {get;set;}
|
||||
|
||||
/// <summary>
|
||||
/// Desc:
|
||||
/// Default:1
|
||||
/// Nullable:True
|
||||
/// </summary>
|
||||
public int? SchoolId {get;set;}
|
||||
|
||||
/// <summary>
|
||||
/// Desc:Student Name
|
||||
/// Default:
|
||||
/// Nullable:True
|
||||
/// </summary>
|
||||
public string Name {get;set;}
|
||||
|
||||
/// <summary>
|
||||
/// Desc:
|
||||
/// Default:DateTime.Now
|
||||
/// Nullable:True
|
||||
/// </summary>
|
||||
public DateTime? CreateTime {get;set;}
|
||||
|
||||
/// <summary>
|
||||
/// Desc:
|
||||
/// Default:
|
||||
/// Nullable:True
|
||||
/// </summary>
|
||||
public byte[] Timestamp {get;set;}
|
||||
|
||||
/// <summary>
|
||||
/// Desc:
|
||||
/// Default:
|
||||
/// Nullable:True
|
||||
/// </summary>
|
||||
public DateTimeOffset? Datetimeoffset {get;set;}
|
||||
|
||||
/// <summary>
|
||||
/// Desc:
|
||||
/// Default:
|
||||
/// Nullable:True
|
||||
/// </summary>
|
||||
public double? Float {get;set;}
|
||||
|
||||
}
|
||||
}
|
@ -5,34 +5,139 @@ using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
|
||||
namespace SugarCodeGeneration
|
||||
{
|
||||
class Program
|
||||
{
|
||||
private const SqlSugar.DbType sqlServer = SqlSugar.DbType.SqlServer;
|
||||
private const string projectName = "SugarCodeGeneration";
|
||||
private const string classPath= "Models";
|
||||
private const string classNamespace = "MyTest";
|
||||
private const string connectionString = "server=.;uid=sa;pwd=@jhl85661501;database=SqlSugar4XTest";
|
||||
|
||||
const SqlSugar.DbType dbType = SqlSugar.DbType.SqlServer;
|
||||
const string connectionString = "server=.;uid=sa;pwd=@jhl85661501;database=SqlSugar4XTest";
|
||||
static List<Task> CsprojList = new List<Task>();
|
||||
static SqlSugar.SqlSugarClient GetDB()
|
||||
{
|
||||
|
||||
return new SqlSugar.SqlSugarClient(new SqlSugar.ConnectionConfig()
|
||||
{
|
||||
DbType = dbType,
|
||||
ConnectionString = connectionString,
|
||||
IsAutoCloseConnection = true,
|
||||
InitKeyType=SqlSugar.InitKeyType.Attribute
|
||||
});
|
||||
}
|
||||
static void Main(string[] args)
|
||||
{
|
||||
//Generation model
|
||||
SqlSugar.SqlSugarClient db = new SqlSugar.SqlSugarClient(new SqlSugar.ConnectionConfig() {
|
||||
DbType = sqlServer,
|
||||
//生成实体
|
||||
GenerationClass();
|
||||
Console.WriteLine("实体创建成功");
|
||||
Console.WriteLine("");
|
||||
|
||||
//生成DbContext
|
||||
GenerationDContext();
|
||||
Console.WriteLine("DbContext创建成功");
|
||||
Console.WriteLine("");
|
||||
|
||||
//生成BLL类
|
||||
GenerationBLL();
|
||||
Console.WriteLine("Bll创建成功");
|
||||
Console.WriteLine("");
|
||||
|
||||
//修改解决方案
|
||||
UpdateCsproj();
|
||||
Console.WriteLine("项目解决方案修改成功");
|
||||
Console.ReadKey();
|
||||
|
||||
//test bll
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 生成BLL
|
||||
/// </summary>
|
||||
private static void GenerationBLL()
|
||||
{
|
||||
var db = GetDB();
|
||||
|
||||
//配置参数
|
||||
var templatePath = Methods.GetCurrentProjectPath + "\\Template\\Bll.txt";//bll模版地址
|
||||
var bllProjectName = "SugarCodeGeneration";//具体项目
|
||||
var bllPath = "BLL";//文件目录
|
||||
var savePath = Methods.GetSlnPath + "\\" + bllProjectName + "\\" + bllPath;//保存目录
|
||||
var tables = db.DbMaintenance.GetTableInfoList().Where(it => it.Name == "Student" || it.Name == "School").Select(it => it.Name).ToList();
|
||||
|
||||
//下面代码不动
|
||||
Methods.CreateBLL(templatePath, savePath, tables);
|
||||
AddTask(bllProjectName, bllPath);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 生成DbContext
|
||||
/// </summary>
|
||||
private static void GenerationDContext()
|
||||
{
|
||||
var db = GetDB();
|
||||
|
||||
|
||||
//配置参数
|
||||
var templatePath = Methods.GetCurrentProjectPath + "\\Template\\DbContext.txt";//dbcontexts模版文件
|
||||
var contextProjectName = "SugarCodeGeneration";//DbContext所在项目
|
||||
var contextPath = "DbContext";//dbcontext存储目录
|
||||
var savePath = Methods.GetSlnPath + "\\" + contextProjectName + "\\" + contextPath+"\\DbContext.cs";//具体文件名
|
||||
var tables = db.DbMaintenance.GetTableInfoList().Where(it => it.Name == "Student" || it.Name == "School").Select(it => it.Name).ToList();
|
||||
|
||||
//下面代码不动
|
||||
var model = new DbContextParameter{
|
||||
ConnectionString = connectionString,
|
||||
IsAutoCloseConnection = true
|
||||
DbType = dbType,
|
||||
Tables = tables
|
||||
};
|
||||
|
||||
|
||||
Methods.CreateDbContext(templatePath,savePath,model);
|
||||
AddTask(contextPath, contextProjectName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成实体类
|
||||
/// </summary>
|
||||
private static void GenerationClass()
|
||||
{
|
||||
|
||||
string classProjectName = "SugarCodeGeneration";//实体类项目名称
|
||||
string classPath = "Models";//生成的目录
|
||||
string classNamespace = "MyTest";//实体命名空间
|
||||
var db = GetDB();
|
||||
var classDirectory = Methods.GetSlnPath + "\\" + classProjectName + "\\" + classPath.TrimStart('\\');
|
||||
|
||||
//如果生成全部可以把Where去掉
|
||||
db.DbFirst.Where("Student", "School").IsCreateAttribute().CreateClassFile(classDirectory, classNamespace);
|
||||
|
||||
AddTask(classPath, classProjectName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改解决方案
|
||||
/// </summary>
|
||||
private static void UpdateCsproj()
|
||||
{
|
||||
foreach (var item in CsprojList)
|
||||
{
|
||||
item.Start();
|
||||
}
|
||||
}
|
||||
|
||||
private static void AddTask(string bllProjectName, string bllPath)
|
||||
{
|
||||
var task = new Task(() =>
|
||||
{
|
||||
Methods.AddCsproj(bllPath, bllProjectName);
|
||||
});
|
||||
var classDirectory = Methods.GetSlnPath +"\\"+projectName+"\\"+ classPath.TrimStart('\\');
|
||||
|
||||
//if all then remove .Where
|
||||
db.DbFirst.Where("Student","School").CreateClassFile(classDirectory, classNamespace);
|
||||
|
||||
Methods.AddCsproj(classPath, projectName);
|
||||
|
||||
//Generation DbContext
|
||||
CsprojList.Add(task);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,8 @@
|
||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<Deterministic>true</Deterministic>
|
||||
<NuGetPackageImportStamp></NuGetPackageImportStamp>
|
||||
<NuGetPackageImportStamp>
|
||||
</NuGetPackageImportStamp>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
@ -48,6 +49,10 @@
|
||||
<Reference Include="Oracle.ManagedDataAccess, Version=4.122.18.3, Culture=neutral, PublicKeyToken=89b483f429c47342, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Oracle.ManagedDataAccess.18.3.0\lib\net40\Oracle.ManagedDataAccess.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="RazorEngine, Version=3.10.0.0, Culture=neutral, PublicKeyToken=9ee697374c7e744a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\RazorEngine.3.10.0\lib\net40\RazorEngine.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="SqlSugar, Version=4.9.7.5, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\sqlSugar.4.9.7.5\lib\SqlSugar.dll</HintPath>
|
||||
</Reference>
|
||||
@ -65,6 +70,10 @@
|
||||
<HintPath>..\packages\System.Data.SQLite.Linq.1.0.109.0\lib\net40\System.Data.SQLite.Linq.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.AspNet.Razor.2.0.30506.0\lib\net40\System.Web.Razor.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
@ -72,9 +81,10 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Models\Student.cs" />
|
||||
<Compile Include="Codes\BLLParameter.cs" />
|
||||
<Compile Include="Codes\FileHelper.cs" />
|
||||
<Compile Include="Codes\DbContextParameter.cs" />
|
||||
<Compile Include="Codes\Methods.cs" />
|
||||
<Compile Include="Models\School.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
@ -84,6 +94,8 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<ItemGroup>
|
||||
<Content Include="Template\BLL.txt" />
|
||||
<Content Include="Template\DbContext.txt" />
|
||||
<Content Include="x64\SQLite.Interop.dll">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
|
7
Src/Asp.Net/SugarCodeGeneration/Template/BLL.txt
Normal file
7
Src/Asp.Net/SugarCodeGeneration/Template/BLL.txt
Normal file
@ -0,0 +1,7 @@
|
||||
using MyTest;
|
||||
public class @(Model.Name)Manager : DbContext<@Model.Name>
|
||||
{
|
||||
|
||||
//我们如果有特殊需要可以重写DbContext中默认 增、删、查、改、方法
|
||||
|
||||
}
|
80
Src/Asp.Net/SugarCodeGeneration/Template/DbContext.txt
Normal file
80
Src/Asp.Net/SugarCodeGeneration/Template/DbContext.txt
Normal file
@ -0,0 +1,80 @@
|
||||
using MyTest;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
public class DbContext<T> where T : class, new()
|
||||
{
|
||||
public DbContext()
|
||||
{
|
||||
Db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
ConnectionString = "@Model.ConnectionString",
|
||||
DbType = DbType.@Model.DbType,
|
||||
InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
|
||||
IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了
|
||||
|
||||
});
|
||||
//调式代码 用来打印SQL
|
||||
Db.Aop.OnLogExecuting = (sql, pars) =>
|
||||
{
|
||||
Console.WriteLine(sql + "\r\n" +
|
||||
Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
||||
Console.WriteLine();
|
||||
};
|
||||
|
||||
}
|
||||
//注意:不能写成静态的
|
||||
public SqlSugarClient Db;//用来处理事务多表查询和复杂的操作
|
||||
public SimpleClient<T> CurrentDb { get { return new SimpleClient<T>(Db); } }
|
||||
|
||||
@foreach(var item in @Model.Tables)
|
||||
{
|
||||
@: public SimpleClient<@item> @(item)Db { get { return new SimpleClient<@item>(Db); } }//用来处理Student表的常用操作
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual List<T> GetList()
|
||||
{
|
||||
return CurrentDb.GetList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据主键删除
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public virtual bool Delete(dynamic id)
|
||||
{
|
||||
return CurrentDb.Delete(id);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 更新
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public virtual bool Update(T obj)
|
||||
{
|
||||
return CurrentDb.Update(obj);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 插入
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public virtual bool Insert(T obj)
|
||||
{
|
||||
return CurrentDb.Insert(obj);
|
||||
}
|
||||
|
||||
//自已扩展更多方法
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="EntityFramework" version="6.2.0" targetFramework="net40" />
|
||||
<package id="Microsoft.AspNet.Razor" version="2.0.30506.0" targetFramework="net40" />
|
||||
<package id="MySql.Data" version="6.7.9" targetFramework="net40" />
|
||||
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net40" />
|
||||
<package id="Oracle.ManagedDataAccess" version="18.3.0" targetFramework="net40" />
|
||||
<package id="RazorEngine" version="3.10.0" targetFramework="net40" />
|
||||
<package id="sqlSugar" version="4.9.7.5" targetFramework="net40" />
|
||||
<package id="System.Data.SQLite" version="1.0.109.2" targetFramework="net40" />
|
||||
<package id="System.Data.SQLite.Core" version="1.0.109.2" targetFramework="net40" />
|
||||
|
Loading…
Reference in New Issue
Block a user