仅在选定的表中使用linq2db中的T4Model生成POCO类

问题描述 投票:0回答:1

我使用linq2db作为我的Web应用程序项目(ASP.NET Core 2.2)与SQL Server数据库的ORM。

该数据库由500多个表组成,只有一部分表与Web应用程序相关。因此,我只想使用T4Model生成映射相关表。有没有办法只为指定的表生成POCO类?

我目前的方法:为所有表生成POCO类,然后删除不需要的表。

t4 poco linq2db
1个回答
1
投票

检查文档https://github.com/linq2db/linq2db/tree/master/Source/LinqToDB.Templates#example-of-generation-process-customization的这一部分

您需要将此代码添加到T4模板以通过Tables字典并删除所有不需要的表,包括与此类表和可能返回它们的过程的关联。例如。

var allowedTables = new HashSet<string>() { "Patient", "Person"  };

// go though Tables and remove all tables you don't need
foreach (var kvp in Tables.ToList())
    if (!allowedTables.Contains(kvp.Value.TableName))
        Tables.Remove(kvp.Key); // remove table
    else
        // if table needed, check that it doesn't have associations to removed tables
        foreach (var keyKvp in kvp.Value.ForeignKeys.ToList())
            if (!allowedTables.Contains(keyKvp.Value.OtherTable.TableName))
                kvp.Value.ForeignKeys.Remove(keyKvp.Key); // remove association to table

// also remove all procedures that return filtered-out tables
foreach (var kvp in Procedures.ToList())
    if (kvp.Value.ResultTable != null && !allowedTables.Contains(kvp.Value.ResultTable.TableName))
        Tables.Remove(kvp.Key); // remove procedure

不幸的是,它仍然可以产生一些剩余物,但你可以用类似的方式过滤掉它们。

© www.soinside.com 2019 - 2024. All rights reserved.