除了数据库实体模型,其余模板准备ok
continuous-integration/drone/push Build is passing

This commit is contained in:
hang
2024-09-05 23:26:23 +08:00
parent 753941204f
commit f0f688a1a3
5 changed files with 132 additions and 34 deletions
+102 -29
View File
@@ -6,6 +6,7 @@ using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
partial class Program
@@ -48,34 +49,9 @@ partial class Program
#region
var contextOptions = new DbContextOptionsBuilder<IRaCISDBContext>().UseSqlServer(@"Server=106.14.89.110,1435;Database=Test_IRC;User ID=sa;Password=xc@123456;TrustServerCertificate=true").Options;
using var dbContext = new IRaCISDBContext(contextOptions);
var dbModel = dbContext.Model;
foreach (var entityType in dbModel.GetEntityTypes())
{
string tableName = entityType.GetTableName();
Console.WriteLine($"Table: {tableName}");
foreach (var property in entityType.GetProperties())
{
string columnName = property.GetColumnName();
string dataType = property.ClrType.Name;
bool isNullable = property.IsNullable;
}
}
#endregion
// 要生成的表名数组
var tableNames = new List<string> { "Product"/*, "Order"*/ };
var tableNames = new List<string> { "Subject"/*, "Order"*/ };
try
{
@@ -94,9 +70,13 @@ partial class Program
{
TableName = tableName,
IsPaged = true
IsPaged = true,
TableFieLdList = GetTableFiledList(tableName)
};
var options = new TemplateOptions();
options.MemberAccessStrategy.Register<TableProperty>();
var parser = new FluidParser();
@@ -104,12 +84,13 @@ partial class Program
if (parser.TryParse(source, out var template, out var error))
{
var context = new TemplateContext(model);
var context = new TemplateContext(model, options, true);
//Console.WriteLine(template.Render(context));
string outputFilePath = Path.Combine(outPutTemplateFolderPath, /*folder,*/ $"{fileName.Replace("Entity", tableName)}.cs");
File.WriteAllText(outputFilePath, template.Render(context));
}
else
@@ -139,6 +120,23 @@ partial class Program
//生成的列表是是分页 还是不分页
public bool IsPaged { get; set; }
public DateTime DateTimeNow = DateTime.Now;
public List<TableProperty> TableFieLdList { get; set; }
public List<TableProperty> AddOrUpdateFieldList => TableFieLdList.Where(t => !AddOrUpdateExcludeNameList.Contains(t.FieldName)).ToList();
public List<TableProperty> ViewListFieldList => TableFieLdList.Where(t => ListInCludeNameList.Contains(t.FieldName)).ToList();
public List<TableProperty> QueryListFieldList => TableFieLdList.Where(t => !AddOrUpdateExcludeNameList.Contains(t.FieldName) && t.IsPrimarykey==false).ToList();
//添加和更新模型排除的字段名
public List<string> AddOrUpdateExcludeNameList = new List<string>() { "CreateUserId", "UpdateUserId", "CreateTime", "UpdateTime", "DeleteUserId", "IsDeleted", "DeletedTime" };
public List<string> ListInCludeNameList = new List<string>() { "CreateTime", "UpdateTime" };
// 列表视图模型名称
public string TableNameView => TableName + "View";
//列表查询模型名称
@@ -152,9 +150,84 @@ partial class Program
public string LowercaseQueryableName => $"{char.ToLower(TableName[0]) + TableName.Substring(1)}Queryable";
public DateTime DateTimeNow = DateTime.Now;
}
public class TableProperty
{
public string ColumnName { get; set; }
public string FieldName { get; set; }
public string CSharpType { get; set; }
public bool IsNullable { get; set; }
// 自动赋值 String.Empty 避免 数据库存储Null
public bool IsCSharpString => CSharpType.ToLower() == "string";
public int? MaxLength { get; set; }
public bool IsPrimarykey { get; set; }
}
private static List<TableProperty> GetTableFiledList(string tableName)
{
#region
var contextOptions = new DbContextOptionsBuilder<IRaCISDBContext>().UseSqlServer(@"Server=106.14.89.110,1435;Database=Test_IRC;User ID=sa;Password=xc@123456;TrustServerCertificate=true").Options;
using var dbContext = new IRaCISDBContext(contextOptions);
var dbModel = dbContext.Model;
var entityType = dbModel.GetEntityTypes().Where(t => t.GetTableName() == tableName).FirstOrDefault();
if (entityType != null)
{
var list = entityType.GetProperties().Select(property => new TableProperty()
{
FieldName = property.Name,
ColumnName = property.GetColumnName(),
IsNullable = property.IsNullable,
IsPrimarykey = property.IsKey(),
CSharpType = GetCSharpType(property.ClrType),
}).ToList();
return list;
}
return new List<TableProperty>();
#endregion
}
static string GetCSharpType(Type type)
{
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
var underlyingType = Nullable.GetUnderlyingType(type);
return $"{GetCSharpType(underlyingType)}?";
}
return type switch
{
_ when type == typeof(int) => "int",
_ when type == typeof(long) => "long",
_ when type == typeof(short) => "short",
_ when type == typeof(byte) => "byte",
_ when type == typeof(bool) => "bool",
_ when type == typeof(decimal) => "decimal",
_ when type == typeof(double) => "double",
_ when type == typeof(float) => "float",
_ when type == typeof(Guid) => "Guid",
_ when type == typeof(DateTime) => "DateTime",
_ when type == typeof(string) => "string",
_ => type.Name // 非常见类型,返回 .NET 类型名称
};
}
#region code first