EF代码优先迁移导致CA1701和CA1703

Wer*_*olf 4 code-analysis resx ef-migrations entity-framework-5

在为我的代码优先EF 5上下文启用迁移后,由于迁移历史记录字符串被添加到项目的resx文件,我开始收到TON的CA1701和CA1703代码分析违规.

我不关心禁用CA1701和CA1703,也不想为每个要添加的迁移抑制100条消息.有没有办法将resx的xml文件或单个resx条目标记为// <自动生成/>以便停止发生?如果我必须禁用这两个规则,那么它只是希望这不是唯一合理的答案!

TIA杰森

Luk*_*oid 7

我个人厌倦了手动保持我的抑制最新(2小时后),所以我写了以下T4模板:

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Runtime.Remoting.Messaging" #>
<#@ output extension=".cs" #>
using System.Diagnostics.CodeAnalysis;

<#
    var @namespace = CallContext.LogicalGetData("NamespaceHint");
    var folder = Path.GetDirectoryName(Host.TemplateFile);

    const int timestampLength = 15;
    var timestampWildcards = new string('?', timestampLength);
    var paths = Directory.EnumerateFiles(folder, timestampWildcards + "_*.cs");

    const int timestampUnderscoreLength = timestampLength + 1;

    var classNames = from path in paths
                     let fileName = Path.GetFileNameWithoutExtension(path)
                     where !fileName.EndsWith(".designer", StringComparison.OrdinalIgnoreCase)
                     where fileName.Length> timestampUnderscoreLength 
                     select fileName.Substring(timestampUnderscoreLength);

    foreach(var className in classNames)
    {
        var fullClassName = @namespace + "." + className;
#>
[assembly: SuppressMessage("Microsoft.Naming", "CA1701:ResourceStringCompoundWordsShouldBeCasedCorrectly", Scope = "resource", Target = "<#=fullClassName#>.resources")]
[assembly: SuppressMessage("Microsoft.Naming", "CA1703:ResourceStringsShouldBeSpelledCorrectly", Scope = "resource", Target = "<#=fullClassName#>.resources")]
<#
    }
#>
Run Code Online (Sandbox Code Playgroud)

使用此内容在与迁移相同的文件夹中创建T4模板,生成的代码将自动包含对资源的抑制,例如

[assembly: SuppressMessage("Microsoft.Naming", "CA1701:ResourceStringCompoundWordsShouldBeCasedCorrectly", Scope = "resource", Target = "LzSoftware.Collectables.Domain.EntityFramework.Migrations.InitialCreation.resources")]
[assembly: SuppressMessage("Microsoft.Naming", "CA1703:ResourceStringsShouldBeSpelledCorrectly", Scope = "resource", Target = "LzSoftware.Collectables.Domain.EntityFramework.Migrations.InitialCreation.resources")]
[assembly: SuppressMessage("Microsoft.Naming", "CA1701:ResourceStringCompoundWordsShouldBeCasedCorrectly", Scope = "resource", Target = "LzSoftware.Collectables.Domain.EntityFramework.Migrations.RemovedAdministratorRoleFromSettings.resources")]
[assembly: SuppressMessage("Microsoft.Naming", "CA1703:ResourceStringsShouldBeSpelledCorrectly", Scope = "resource", Target = "LzSoftware.Collectables.Domain.EntityFramework.Migrations.RemovedAdministratorRoleFromSettings.resources")]
Run Code Online (Sandbox Code Playgroud)


Lan*_*eld 6

我今天才注意到这一点,我自己.您可以使用AssemblyInfo.cs或GlobalSuppressions.cs文件中的以下内容在资源级别禁止CA1701和CA1703警告:

[assembly: SuppressMessage("Microsoft.Naming",
    "CA1701:ResourceStringCompoundWordsShouldBeCasedCorrectly",
    Justification = "The auto-genererated code from code first migrations trigger this warning.",
    Scope = "resource", Target = "Full.Namespace.To.Your.Migrations.NameOfYourMigration.resources")]
[assembly: SuppressMessage("Microsoft.Naming",
    "CA1703:ResourceStringsShouldBeSpelledCorrectly",
    Justification = "The auto-genererated code from code first migrations trigger this warning.",
    Scope = "resource", Target = "Full.Namespace.To.Your.Migrations.NameOfYourMigration.resources")]
Run Code Online (Sandbox Code Playgroud)

您需要为每次迁移执行此操作,但这比单独抑制每个警告要好得多.

  • 很棒的答案......最简单的方法就是 - 通过"代码分析"窗口将一个现有警告抑制到globalsuppressions文件中. - 将答案中的代码块复制到globalsuppressions文件中 - 将目标属性的值从抑制的消息替换为上述代码块的目标属性. (2认同)