mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-10-15 18:55:07 +08:00
Update Demo
This commit is contained in:
@@ -35,25 +35,27 @@ namespace SugarCodeGeneration.Codes
|
||||
|
||||
public static void AddCsproj(string classPath, string projectName)
|
||||
{
|
||||
var classDirectory = Methods.GetSlnPath + "\\" +projectName+"\\"+ classPath.TrimStart('\\');
|
||||
if (FileHelper.IsExistDirectory(classDirectory) == false) {
|
||||
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 + @"\"+projectName+".csproj";
|
||||
var files = Directory.GetFiles(classDirectory).ToList().Select(it => classPath + "\\" + Path.GetFileName(it));
|
||||
var xmlPath = GetSlnPath + @"\" + projectName + @"\" + projectName + ".csproj";
|
||||
|
||||
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();
|
||||
var compieList=itemGroup.Elements().ToList();
|
||||
var itemGroup = xe.Root.Elements().Where(it => it.Name.LocalName == "ItemGroup" && it.Elements().Any(y => y.Name.LocalName == "Compile")).First();
|
||||
var compieList = itemGroup.Elements().ToList();
|
||||
var noAddFiles = files.Where(it => !compieList.Any(f => it.Equals(f.Attribute("Include").Value, StringComparison.CurrentCultureIgnoreCase))).ToList();
|
||||
if (noAddFiles.Any()) {
|
||||
if (noAddFiles.Any())
|
||||
{
|
||||
foreach (var item in noAddFiles)
|
||||
{
|
||||
var addItem = new XElement("Compile", new XAttribute("Include",item));
|
||||
itemGroup.AddFirst(addItem) ;
|
||||
var addItem = new XElement("Compile", new XAttribute("Include", item));
|
||||
itemGroup.AddFirst(addItem);
|
||||
}
|
||||
}
|
||||
newXml = xe.ToString().Replace("xmlns=\"\"", "");
|
||||
@@ -61,7 +63,7 @@ namespace SugarCodeGeneration.Codes
|
||||
xe.Save(xmlPath);
|
||||
}
|
||||
|
||||
public static void CreateBLL(string templatePath, string savePath,List<string> tables)
|
||||
public static void CreateBLL(string templatePath, string savePath, List<string> tables)
|
||||
{
|
||||
|
||||
string template = System.IO.File.ReadAllText(templatePath); //从文件中读出模板内容
|
||||
@@ -70,18 +72,29 @@ namespace SugarCodeGeneration.Codes
|
||||
{
|
||||
BLLParameter model = new BLLParameter()
|
||||
{
|
||||
Name=item
|
||||
Name = item
|
||||
};
|
||||
var result = Engine.Razor.RunCompile(template, templateKey, model.GetType(), model);
|
||||
FileHelper.CreateFile(savePath+"\\"+item+ "Manager.cs", result, System.Text.Encoding.UTF8);
|
||||
var cp = savePath + "\\" + item + "Manager.cs";
|
||||
if (FileHelper.IsExistFile(cp) == false)
|
||||
FileHelper.CreateFile(cp, result, System.Text.Encoding.UTF8);
|
||||
}
|
||||
}
|
||||
|
||||
public static void CreateDbContext(string templatePath, string savePath, object model) {
|
||||
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);
|
||||
string templateKey = "dbcontext"; //取个名字
|
||||
var result = Engine.Razor.RunCompile(template, templateKey, model.GetType(), model);
|
||||
FileHelper.CreateFile(savePath, result, System.Text.Encoding.UTF8);
|
||||
}
|
||||
|
||||
|
||||
public static void CreateProject(string name) {
|
||||
var templatePath = GetCurrentProjectPath + "/Template/Project.txt";
|
||||
string project = System.IO.File.ReadAllText(templatePath).Replace("@pid",Guid.NewGuid().ToString()); //从文件中读出模板内容
|
||||
//FileHelper
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -12,69 +12,81 @@ namespace SugarCodeGeneration
|
||||
{
|
||||
class Program
|
||||
{
|
||||
|
||||
//数据库配置
|
||||
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)
|
||||
{
|
||||
//生成实体
|
||||
GenerationClass();
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("实体创建成功");
|
||||
Console.WriteLine("");
|
||||
/***连接数据库***/
|
||||
var db = GetDB();
|
||||
|
||||
//生成DbContext
|
||||
GenerationDContext();
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("DbContext创建成功");
|
||||
Console.WriteLine("");
|
||||
|
||||
//生成BLL类
|
||||
GenerationBLL();
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("Bll创建成功");
|
||||
Console.WriteLine("");
|
||||
/***生成实体***/
|
||||
|
||||
//修改解决方案
|
||||
//配置参数
|
||||
string classProjectName = "SugarCodeGeneration";//实体类项目名称
|
||||
string classPath = "Models";//生成的目录
|
||||
string classNamespace = "MyTest";//实体命名空间
|
||||
var classDirectory = Methods.GetSlnPath + "\\" + classProjectName + "\\" + classPath.TrimStart('\\');
|
||||
//执行生成
|
||||
GenerationClass(classProjectName, classPath, classNamespace, classDirectory);
|
||||
Print("实体创建成功");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/***生成DbContext***/
|
||||
|
||||
//配置参数
|
||||
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().Select(it => it.Name).ToList();
|
||||
//执行生成
|
||||
GenerationDContext(templatePath, contextProjectName, contextPath, savePath, tables);
|
||||
Print("DbContext创建成功");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/***生成BLL***/
|
||||
|
||||
//配置参数
|
||||
var templatePath2 = Methods.GetCurrentProjectPath + "\\Template\\Bll.txt";//bll模版地址
|
||||
var bllProjectName2 = "SugarCodeGeneration";//具体项目
|
||||
var bllPath2 = "BLL";//文件目录
|
||||
var savePath2 = Methods.GetSlnPath + "\\" + bllProjectName2 + "\\" + bllPath2;//保存目录
|
||||
var tables2 = db.DbMaintenance.GetTableInfoList().Select(it => it.Name).ToList();
|
||||
//执行生成
|
||||
GenerationBLL(templatePath2,bllProjectName2,bllPath2,savePath2, tables2);
|
||||
Print("BLL创建成功");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/***修改解决方案***/
|
||||
UpdateCsproj();
|
||||
Console.WriteLine("项目解决方案修改成功");
|
||||
Console.ReadKey();
|
||||
Print("项目解决方案修改成功");
|
||||
|
||||
//test bll
|
||||
//如何使用创建好的业务类(注意 SchoolManager 不能是静态的)
|
||||
//SchoolManager sm = new SchoolManager();
|
||||
//sm.GetList();
|
||||
//sm.StudentDb.AsQueryable().Where(it => it.Id == 1).ToList();
|
||||
//sm.Db.Queryable<Student>().ToList();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
|
||||
/// <summary>
|
||||
/// 生成BLL
|
||||
/// </summary>
|
||||
private static void GenerationBLL()
|
||||
private static void GenerationBLL(string templatePath, string bllProjectName, string bllPath, string savePath, List<string>tables)
|
||||
{
|
||||
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);
|
||||
@@ -85,26 +97,14 @@ namespace SugarCodeGeneration
|
||||
/// <summary>
|
||||
/// 生成DbContext
|
||||
/// </summary>
|
||||
private static void GenerationDContext()
|
||||
private static void GenerationDContext(string templatePath, string contextProjectName, string contextPath, string savePath,List<string> tables)
|
||||
{
|
||||
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,
|
||||
DbType = dbType,
|
||||
Tables = tables
|
||||
};
|
||||
|
||||
|
||||
Methods.CreateDbContext(templatePath,savePath,model);
|
||||
AddTask(contextProjectName,contextPath);
|
||||
}
|
||||
@@ -112,21 +112,16 @@ namespace SugarCodeGeneration
|
||||
/// <summary>
|
||||
/// 生成实体类
|
||||
/// </summary>
|
||||
private static void GenerationClass()
|
||||
private static void GenerationClass(string classProjectName,string classPath,string classNamespace,string classDirectory)
|
||||
{
|
||||
|
||||
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);
|
||||
|
||||
//下面代码不动
|
||||
db.DbFirst.IsCreateAttribute().CreateClassFile(classDirectory, classNamespace);
|
||||
AddTask(classProjectName,classPath);
|
||||
}
|
||||
|
||||
#region 辅助方法
|
||||
/// <summary>
|
||||
/// 修改解决方案
|
||||
/// </summary>
|
||||
@@ -138,6 +133,12 @@ namespace SugarCodeGeneration
|
||||
item.Wait();
|
||||
}
|
||||
}
|
||||
private static void Print(string message)
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine(message);
|
||||
Console.WriteLine("");
|
||||
}
|
||||
|
||||
private static void AddTask(string bllProjectName, string bllPath)
|
||||
{
|
||||
@@ -147,5 +148,18 @@ namespace SugarCodeGeneration
|
||||
});
|
||||
CsprojList.Add(task);
|
||||
}
|
||||
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
|
||||
});
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@@ -53,6 +53,10 @@
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.ComponentModel.DataAnnotations" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Data.SQLite, Version=1.0.102.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>xSqlite\System.Data.SQLite.dll</HintPath>
|
||||
</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>
|
||||
@@ -79,12 +83,14 @@
|
||||
<ItemGroup>
|
||||
<Content Include="Template\BLL.txt" />
|
||||
<Content Include="Template\DbContext.txt" />
|
||||
<Content Include="Template\Project.txt" />
|
||||
<Content Include="x64\SQLite.Interop.dll">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="x86\SQLite.Interop.dll">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="xSqlite\System.Data.SQLite.dll" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
@@ -4,4 +4,9 @@ public class @(Model.Name)Manager : DbContext<@Model.Name>
|
||||
|
||||
//我们如果有特殊需要可以重写DbContext中默认 增、删、查、改、方法
|
||||
|
||||
|
||||
//这里面写的代码不会给覆盖,如果要重新生成请删除 @(Model.Name)Manager.cs
|
||||
|
||||
|
||||
|
||||
}
|
59
Src/Asp.Net/SugarCodeGeneration/Template/Project.txt
Normal file
59
Src/Asp.Net/SugarCodeGeneration/Template/Project.txt
Normal file
@@ -0,0 +1,59 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="11.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>@pid</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>ClassLibrary1</RootNamespace>
|
||||
<AssemblyName>ClassLibrary1</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System"/>
|
||||
|
||||
<Reference Include="System.Core"/>
|
||||
<Reference Include="System.Xml.Linq"/>
|
||||
<Reference Include="System.Data.DataSetExtensions"/>
|
||||
|
||||
|
||||
<Reference Include="Microsoft.CSharp"/>
|
||||
|
||||
<Reference Include="System.Data"/>
|
||||
|
||||
<Reference Include="System.Xml"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Class1.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
|
||||
</Project>
|
BIN
Src/Asp.Net/SugarCodeGeneration/xSqlite/System.Data.SQLite.dll
Normal file
BIN
Src/Asp.Net/SugarCodeGeneration/xSqlite/System.Data.SQLite.dll
Normal file
Binary file not shown.
Reference in New Issue
Block a user