4

コード ファーストの EF 5 コンテキストの移行を有効にすると、移行履歴文字列がプロジェクトの resx ファイルに追加されるため、CA1701 および CA1703 コード分析違反のトンを受け取るようになりました。

CA1701 と CA1703 を無効にすることは気にしません。また、追加される個々の移行ごとに 100 のようなメッセージを抑制したくありません。resx の xml ファイルまたは個々の resx エントリを // <自動生成 /> としてマークして、これが起こらないようにする方法はありますか? 2 つのルールを無効にする必要がある場合は、それが唯一の正気の答えではないことを願っています。

ティア・ジェイソン

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 に答える