FreeTool 1.3.2026.527

dotnet tool install --global FreeTool --version 1.3.2026.527
                    
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest
                    
if you are setting up this repo
dotnet tool install --local FreeTool --version 1.3.2026.527
                    
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=FreeTool&version=1.3.2026.527
                    
nuke :add-package FreeTool --version 1.3.2026.527
                    

FreeTool

XML 转 FreeSql 实体类工具

FreeTool 是一个 .NET CLI 工具,读取 XML 模型定义文件,自动生成带有 FreeSql.DataAnnotations 特性的 C# 实体类。


安装

全局安装(推荐)

dotnet tool install --global FreeTool

安装后,在任意目录直接执行:

FreeTool

本地安装

dotnet tool install --local FreeTool

临时运行(不安装)

dotnet run --project <FreeTool.csproj 路径> --framework net8.0

使用方法

基本用法

将 XML 模型文件放在当前目录,直接运行工具:

FreeTool

工具会自动读取当前目录下所有 *.xml 文件,并生成实体类。

如果当前目录没有 *.xml 文件,工具会尝试读取 model.xml

命令行参数(可选,用于覆盖 XML 配置)

参数 说明 默认值
-NameSpace 生成实体类的命名空间 读取 XML <Namespace>,否则为 FreeEntity
-Output 实体类输出目录 读取 XML <Output>,否则为当前目录

示例:

FreeTool -NameSpace MyProject.Entity -Output ./Entities

配置优先级: 命令行参数 > XML <Option> > 内置默认值


XML 模型文件格式

完整示例

<?xml version="1.0" encoding="utf-8"?>
<EntityModel xmlns="https://newlifex.com/Model202509.xsd">
  <Option>
    
    <Namespace>MyProject.Entity</Namespace>
    
    <Output>./Entities</Output>
    
    <BaseClass>EntityBase</BaseClass>
  </Option>

  <Table Name="User" Description="用户表" BaseType="EntityBase">
    <Column Name="Id" DataType="Int32" PrimaryKey="True" Identity="True" Description="主键" />
    <Column Name="Name" DataType="String" Length="50" Nullable="False" Description="用户名" />
    <Column Name="Email" DataType="String" Length="100" Description="邮箱" />
    <Column Name="CreateTime" DataType="DateTime" Description="创建时间" />

    <Index Columns="Name" Unique="true" />
    <Index Columns="Email" />
  </Table>
</EntityModel>

命名空间

XML 根元素必须使用命名空间:

xmlns="https://newlifex.com/Model202509.xsd"

Option 节点

定义全局配置:

子元素 说明 默认值
<Namespace> 生成实体类的命名空间 FreeEntity
<Output> 实体类输出目录 当前目录
<BaseClass> 全局基类(可被 Table 的 BaseType 覆盖)

Table 节点

定义一张数据表:

属性 说明 必填
Name 表名(类名)
Description 表注释
BaseType 当前表的基类(覆盖全局 BaseClass

Column 节点

定义字段:

属性 说明 示例
Name 字段名 Id
Description 字段注释 主键
DataType 数据类型 Int32StringDateTime
Type 直接指定 C# 类型(覆盖 DataType intstring
Length 长度 50-1
PrimaryKey 是否主键 True / False
Identity 是否自增 True / False
Nullable 是否可空 True / False

Index 节点

定义索引:

属性 说明 示例
Columns 索引字段,多个用逗号分隔 Name / DeptId,Name
Unique 是否唯一索引 true / false

DataType 映射表

XML DataType C# 类型
Int64 long
Int32 int
Int16 short
Byte byte
String string
DateTime DateTime
DateTimeOffset DateTimeOffset
Double double
Single float
Boolean bool
Decimal decimal
Guid Guid
Byte[] byte[]
其他 object

生成文件示例

以上 User 表示例会生成 User.cs

using FreeSql.DataAnnotations;
using Newtonsoft.Json;

namespace MyProject.Entity
{
    /// <summary>
    /// 用户表
    /// </summary>
    [Table(Name = "User")]
    [JsonObject(MemberSerialization.OptIn)]
    [Index("IX_User_Name", "Name", IsUnique = true)]
    public partial class User : EntityBase
    {
        /// <summary>
        /// 主键
        /// </summary>
        [JsonProperty]
        [Column(IsPrimary = true, IsIdentity = true)]
        public int Id { get; set; }

        /// <summary>
        /// 用户名
        /// </summary>
        [JsonProperty]
        [Column(IsNullable = false, StringLength = 50)]
        public string Name { get; set; }

        /// <summary>
        /// 邮箱
        /// </summary>
        [JsonProperty]
        [Column(StringLength = 100)]
        public string Email { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        [JsonProperty]
        public DateTime CreateTime { get; set; }
    }
}

多 XML 文件处理

当前目录下若存在多个 .xml 文件,工具会逐一处理

FreeTool
# 处理 Model1.xml Model2.xml

每个 XML 文件独立使用自身的 <Option> 配置。


注意事项

  1. 输出目录不存在时会自动创建
  2. 同名文件会被覆盖,请确保输出目录不是源码目录
  3. 生成的实体类依赖 FreeSql.DataAnnotationsNewtonsoft.Json,请在目标项目中安装这两个包
  4. 工具本身运行时不需要引用 FreeSqlNewtonsoft.Json
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

Version Downloads Last Updated
1.3.2026.527 48 5/27/2026
1.2.2026.527 48 5/27/2026
1.1.2026.527 52 5/27/2026
1.0.2026.527 75 5/27/2026