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()
}

本文作者:KaryoYou

本文链接:https://www.cnblogs.com/KaryoYou/p/18279727/FreeSqlGenerator

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   KaryoYou  阅读(33)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 世界が终るまでは…《直到世界尽头》 直到世界尽头《世界が終るまでは…》
世界が终るまでは…《直到世界尽头》 - 直到世界尽头《世界が終るまでは…》
00:00 / 00:00
An audio error has occurred.

作词 : 上杉昇

大都会に 僕はもう一人で

投げ捨てられた 空きカンのようだ

互いのすべてを 知りつくすまでが

愛ならば いっそ 永久(とわ)に眠ろうか

世界が終るまでは

離れる事もない

そう願っていた

幾千の夜と

戻らない時だけが

何故輝いては

やつれ切った 心までも 壊す

はかなき想い

このTragedy Night

かけがえのない 何かを失う

そして人は 形(こたえ)を求めて

かけがえのない 何かを失う

欲望だらけの 街じゃ 夜空の

星屑も 僕らを 灯せない

世界が終る前に

聞かせておくれよ

満開の花が

似合いのCatastrophe

誰もが望みながら 永遠を信じない

なのに きっと 明日を夢見てる

はかなき日々と

このTragedy Night

そう願っていた 幾千の夜と

世界が終るまでは 離れる事もない

そう願っていた 幾千の夜と

戻らない時だけが 何故輝いては

やつれ切った 心までも 壊す

はかなき想い

このTragedy Night

このTragedy Night

(终)