安装和使用方法:传送门(大佬的学习笔记)
dotnet tool install -g FreeSql.Generator
.bat文件:__重新生成.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()
}
标签:实体类,string,Generator,C#,FreeSql,System,col,using,gen
From: https://www.cnblogs.com/KaryoYou/p/18279727/FreeSqlGenerator