如何将 CSharpEntityTypeGenerator 拉入项目?

How do I pull CSharpEntityTypeGenerator into the project?

我看到很多帖子都在谈论如何通过 subclass CSharpEntityTypeGenerator 来修改 EF Core 写出的内容。 Visual studio 不喜欢,说要安装 Microsoft.EntityFrameworkCore.Design。我已经安装了 v3.1.0-preview1.19506.2.

但是,VS 仍然说找不到 class 并无法安装 NuGet 包。

使这项工作有什么魔力?

我想写一个这样的 class 以便添加 #nullable disable 并插入 GeneratedCode 属性。

public class EntityTypeGenerator : CSharpEntityTypeGenerator
{
    public EntityTypeGenerator(ICSharpHelper helper) : base(helper) { }

    public override string WriteCode(IEntityType type, string @namespace, bool useDataAnnotations)
    {
        var code = base.WriteCode(type, @namespace, useDataAnnotations);

        var old = "public partial class " + type.Name;
        var updated = "[System.CodeDom.Compiler.GeneratedCode]\n" + old;

        return code.Replace(old, updated).Replace("namespace", "#nullable disable\n\nnamespace");
    }
}

更新项目文件中的引用:

<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0">
  <PrivateAssets>all</PrivateAssets>
  <!-- Remove IncludeAssets to allow compiling against the assembly -->
  <!--<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>-->
</PackageReference>