C# 数据实体类生成工具 FreeSql.Generator

安装和使用方法:传送门(大佬的学习笔记)

dotnet tool install -g FreeSql.Generator

.bat文件:__重新生成.bat

建议重命名为:__重新生成2.bat
只要不同就行,避免运行后被覆盖。

FreeSql.Generator -Razor "__razor.cshtml.txt" -NameOptions 1,0,0,1 -NameSpace MyProject -DB "SqlServer,Data Source=192.168.1.1,1433;Initial Catalog=erp;User ID=sa;Password=admin;Pooling=true;Max Pool Size=5" -FileName "{name}.cs"
pause

.text文件:__razor.cshtml.txt

该模板由这位大佬帮忙修改的:Memoyu
当时是在Gitee下载的FreeSql.Tools的项目自定义的模板,可以直接复制内容过来粘贴使用。

@using FreeSql.DatabaseModel;@{
var gen = Model as RazorModel;

Func<string, string> GetAttributeString = attr => {
	if (string.IsNullOrEmpty(attr)) return null;
	return string.Concat(", ", attr.Trim('[', ']'));
};
Func<DbColumnInfo, string> GetDefaultValue = col => {
    if (col.CsType == typeof(string)) return " = string.Empty;";
    return "";
};
}@{
switch (gen.fsql.Ado.DataType) {
	case FreeSql.DataType.PostgreSQL:
@:using System;
@:using System.Collections;
@:using System.Collections.Generic;
@:using System.Linq;
@:using System.Reflection;
@:using System.Threading.Tasks;
@:using Newtonsoft.Json;
@:using FreeSql.DataAnnotations;
@:using System.Net;
@:using Newtonsoft.Json.Linq;
@:using System.Net.NetworkInformation;
@:using NpgsqlTypes;
@:using Npgsql.LegacyPostgis;
		break;
	case FreeSql.DataType.SqlServer:
	case FreeSql.DataType.MySql:
	default:
@:using System;
@:using System.Collections;
@:using System.Collections.Generic;
@:using System.Linq;
@:using System.Reflection;
@:using System.Threading.Tasks;
@:using FreeSql.DataAnnotations;
		break;
}
}
namespace @gen.NameSpace {

@if (string.IsNullOrEmpty(gen.table.Comment) == false) {
	@:/// <summary>
	@:/// @gen.table.Comment.Replace("\r\n", "\n").Replace("\n", "\r\n		/// ")
	@:/// </summary>
}
	public partial class @gen.GetCsName(gen.FullTableName) {

	@foreach (var col in gen.columns) {

		if (string.IsNullOrEmpty(col.Coment) == false) {
		@:/// <summary>
		@:/// @col.Coment.Replace("\r\n", "\n").Replace("\n", "\r\n		/// ")
		@:/// </summary>
		}
		@:@(gen.GetColumnAttribute(col))
		@:public @gen.GetCsType(col) @gen.GetCsName(col.Name) { get; set; }@GetDefaultValue(col)
@:
	}
	}
@gen.GetMySqlEnumSetDefine()
}

追加更新的自定义模板:2024/07/17

因为某些个人癖好,字段生成的属性命名不喜欢,后续多次手动编辑[Column(Name="原字段名")]太过麻烦。干脆直接每个属性都加上原有字段名的定义,这样,后续编辑实体实例时,省去重复添加指定字段名的操作。字段名写在上面也能明确这个属性在数据库中的字段列名就是这个*样。

@using FreeSql.DatabaseModel;
@{
	var gen = Model as RazorModel;

	Func<string, string> GetAttributeString = attr =>
	{
		if (string.IsNullOrEmpty(attr)) return null;
		return string.Concat(", ", attr.Trim('[', ']'));
	};
	Func<DbColumnInfo, string> GetDefaultValue = col =>
	{
		if (col.CsType == typeof(string)) return " = string.Empty;";
		return "";
	};
	Func<DbColumnInfo, string> GetAttributeString2 = col =>
	{
		var colName = col.Name;
		var csName = gen.GetCsName(col.Name);
		var attribute = gen.GetColumnAttribute(col);
		if (string.IsNullOrEmpty(attribute) == true)
		{
			attribute = $"[Column(Name = \"{colName }\")]";
		}
		else if (colName == csName)
		{
			attribute = attribute.Replace("[Column(","");
			attribute = $"[Column(Name = \"{colName}\", {attribute}";
		}
		return attribute;
		};
}
@{
	switch (gen.fsql.Ado.DataType)
	{
		case FreeSql.DataType.PostgreSQL:
@:using System;
@:using System.Collections;
@:using System.Collections.Generic;
@:using System.Linq;
@:using System.Reflection;
@:using System.Threading.Tasks;
@:using Newtonsoft.Json;
@:using FreeSql.DataAnnotations;
@:using System.Net;
@:using Newtonsoft.Json.Linq;
@:using System.Net.NetworkInformation;
@:using NpgsqlTypes;
@:using Npgsql.LegacyPostgis;
			break;
		case FreeSql.DataType.SqlServer:
		case FreeSql.DataType.MySql:
		default:
@:using System;
@:using System.Collections;
@:using System.Collections.Generic;
@:using System.Linq;
@:using System.Reflection;
@:using System.Threading.Tasks;
@:using FreeSql.DataAnnotations;
			break;
	}
}
namespace @gen.NameSpace
{

@if (string.IsNullOrEmpty(gen.table.Comment) == false)
{
	@:/// <summary>
	@:/// @gen.table.Comment.Replace("\r\n", "\n").Replace("\n", "\r\n		/// ")
	@:/// </summary>
    	}
	public partial class @gen.GetCsName(gen.FullTableName)
	{
	@foreach (var col in gen.columns)
	{
		if (string.IsNullOrEmpty(col.Coment) == false)
		{
		@:/// <summary>
		@:/// @col.Coment.Replace("\r\n", "\n").Replace("\n", "\r\n		/// ")
		@:/// </summary>
		}
		@:@GetAttributeString2(col)
		@:public @gen.GetCsType(col) @gen.GetCsName(col.Name) { get; set; }@GetDefaultValue(col)
@:
	}
	}
@gen.GetMySqlEnumSetDefine()
}
posted @ 2024-07-02 13:38  KaryoYou  阅读(117)  评论(0)    收藏  举报