gpt4 book ai didi

c# - 网络核心 : Create Generic Repository Interface Id Mapping for All Tables Auto Code Generation

转载 作者:太空狗 更新时间:2023-10-30 01:12:38 25 4
gpt4 key购买 nike

我们只是搭建了我们的数据库,并从 Entity Framework 中的数据库表创建了模型。

此外,我们正在创建具有映射到主键的 ID 的文件。这样做的目的是映射到我们使用 Id 的通用存储库接口(interface)。

我如何浏览我所有的 200 多个模型,并创建一个类似于下面文件 2 的文件。我已经在以前的工作场所看到过它。正在努力研究。是否有自动循环遍历所有模型的 Visual Studio 或 Entity Framework 功能?目前,我正在研究每个模型,并手动创建 ID,如通用 ID 文件 2 中所示。愿意实现实现代码生成的 T4,但其他解决方案也不错。

脚手架文件 1:

namespace Datatest
{
public partial class Property
{
public int Property { get; set; }
public int DocumentId { get; set; }
public string Address { get; set; }
}
}

通用 ID 文件 2:

public partial class Property: IEntity
{
[NotMapped]
public int Id { get => PropertyId; set => PropertyId = value; }
}

所有表的示例通用基础存储库:

    public T Get(int id)
{
return Table.Find(id);
}
public async Task<T> GetAsync(int id)
{
return await Table.FindAsync(id);
}
public T Single(Expression<Func<T, bool>> predicate)
{
return All.Single(predicate);
}
public async Task<T> SingleAsync(Expression<Func<T, bool>> predicate)
{
return await All.SingleAsync(predicate);
}
public T FirstOrDefault(int id)
{
return All.FirstOrDefault(CreateEqualityExpressionForId(id));
}

也许这个资源有帮助?现在试着让它循环遍历我所有的模型文件 How to create multiple output files from a single T4 template using Tangible Editor?

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ output extension=".txt" #>
<#
for (Int32 i = 0; i < 10; ++i) {
#>
Content <#= i #>
<#
// End of file.
SaveOutput("Content" + i.ToString() + ".cs");
}
#>
<#+
private void SaveOutput(string outputFileName) {
string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
string outputFilePath = Path.Combine(templateDirectory, outputFileName);
File.WriteAllText(outputFilePath, this.GenerationEnvironment.ToString());
this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);
}
#>

最佳答案

我同意@Ivan 的观点,我不会向您推荐这种方式,但您回答说您需要这样做,所以我们开始吧。

您使用的是 EFCore 吗?幸运的是,EFCore 是开源的,因此我们可以深入研究源代码并构建自定义 EFCore 版本。

几个月前,我对 EF Context 脚手架也有特殊需求,我们还有 200 多个表,需要将每个表的映射放在一个单独的类中,因为 EF Core 默认将所有映射内容放在DbContext 文件,这为我们生成了 10k+ 行代码长的 DbContext 类😱。

EntityTypes 生成处理 here . #109 对你来说很有趣:

_sb.AppendLine($"public partial class {entityType.Name}");

在这里你可以这样添加你的界面:

_sb.AppendLine($"public partial class {entityType.Name} : IEntity");

然后我们必须在 #113 上实现您的接口(interface)我们有以下代码:

using (_sb.Indent())
{
GenerateConstructor(entityType);
GenerateProperties(entityType);
GenerateNavigationProperties(entityType);
}

就在 GenerateProperties(entityType); 之前,您可以添加以下行来实现接口(interface)规范:

_sb.AppendLine("[NotMapped]");
_sb.AppendLine("public int Id { get => PropertyId; set => PropertyId = value; }");
_sb.AppendLine("");

如果您真的需要/想要部分类,您可以简单地编写一些代码来在 WriteCode 中生成另一个文件。每个表调用一次的方法,并具有此方法所需的所有信息(类型名称等)

Here是使用自定义实现构建项目的代码。搭建项目脚手架后,您可以返回到官方 EFCore 构建。

关于c# - 网络核心 : Create Generic Repository Interface Id Mapping for All Tables Auto Code Generation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57261804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com