简体   繁体   English

如何添加//<auto-generated /> 自动生成文件? 更具体的迁移

[英]How to add //<auto-generated /> automatically to generated files? More specifically migrations

So i have my migrations folder from ef core right?所以我有来自 ef core 的迁移文件夹,对吗? But my IDE style code throws a bunch of errors in the generated files, so i need to manually insert the // comment, so everything is ignored.但是我的 IDE 样式代码在生成的文件中抛出了一堆错误,所以我需要手动插入 // 注释,所以一切都被忽略了。 Is there any way, that whenever a new migration is added to add this automatically?有什么办法,每当添加新迁移时自动添加它?

You can do this by creating your own custom CSharpDbContextGenerator and CSharpEntityTypeGenerator classes.您可以通过创建自己的自定义CSharpDbContextGeneratorCSharpEntityTypeGenerator类来执行此操作。 For example:例如:

public class MyContextTypeGenerator : CSharpDbContextGenerator
{
    public MyContextTypeGenerator(
        IProviderConfigurationCodeGenerator providerConfigurationCodeGenerator,
        IAnnotationCodeGenerator annotationCodeGenerator,
        ICSharpHelper cSharpHelper) : base(providerConfigurationCodeGenerator, annotationCodeGenerator, cSharpHelper)
    {
    }

    public override string WriteCode(
        IModel model,
        string contextName,
        string connectionString,
        string contextNamespace,
        string modelNamespace,
        bool useDataAnnotations,
        bool useNullableReferenceTypes,
        bool suppressConnectionStringWarning,
        bool suppressOnConfiguring)
    {
        var builder = new IndentedStringBuilder();

        // Add your own code to the start
        builder.AppendLine("// <auto-generated>");

        // Add the default code be generated
        builder.Append(base.WriteCode(model, contextName, connectionString, contextNamespace, modelNamespace, useDataAnnotations, useNullableReferenceTypes, suppressConnectionStringWarning, suppressOnConfiguring));

        return builder.ToString();
    }
}

And for the entities:对于实体:

public class MyEntityTypeGenerator : CSharpEntityTypeGenerator
{
    public MyEntityTypeGenerator(IAnnotationCodeGenerator annotationCodeGenerator, ICSharpHelper cSharpUtilities)
        : base(annotationCodeGenerator, cSharpUtilities)
    {
    }


    public override string WriteCode(IEntityType entityType, string @namespace, bool useDataAnnotations, bool useNullableReferenceTypes)
    {
        var builder = new StringBuilder();

        builder.AppendLine("// <auto-generated>");

        builder.Append(base.WriteCode(entityType, @namespace, useDataAnnotations, useNullableReferenceTypes));

        return builder.ToString();

    }
}

Then you need to ensure the types gets used by EF, so add this class too:然后您需要确保 EF 使用这些类型,因此还要添加此 class:

public class MyDesignTimeServices : IDesignTimeServices
{
    public void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
    {
        serviceCollection.AddSingleton<ICSharpDbContextGenerator, MyContextTypeGenerator>();
        serviceCollection.AddSingleton<ICSharpEntityTypeGenerator, MyEntityTypeGenerator>();

    }
}

Note 1 : You probably also need to add the Microsoft.EntityFrameworkCore.Design Nuget package.注意 1 :您可能还需要添加Microsoft.EntityFrameworkCore.Design Nuget package。

Note 2 : This will give you a warning about using EF Core internal classes which you can disable with:注意 2 :这将向您发出有关使用 EF Core 内部类的警告,您可以通过以下方式禁用这些类:

#pragma warning disable EF1001

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM