4

在为我的代码优先 EF 5 上下文启用迁移后,由于将迁移历史字符串添加到项目的 resx 文件中,我开始收到大量 CA1701 和 CA1703 代码分析违规行为。

我不想禁用 CA1701 和 CA1703,也不想为每个要添加的单独迁移抑制 100 条消息。有没有办法将 resx 的 xml 文件或单个 resx 条目标记为 // <auto-generated /> 这样就不会发生这种情况了?如果我必须禁用这两个规则,那么只是希望这不是唯一理智的答案!

TIA杰森

4

2 回答 2

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")]
<#
    }
#>

在与迁移相同的文件夹中创建一个包含此内容的 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")]
于 2014-06-02T21:29:23.690 回答
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")]

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

于 2013-09-09T23:06:01.777 回答