4

啓用我的代碼優先EF 5上下文的遷移後,我開始接收到由於將遷移歷史字符串添加到項目的resx文件而導致的CA1701和CA1703代碼分析違例的TON。EF代碼首先遷移導致CA1701和CA1703

我不在乎禁用CA1701和CA1703,也不想爲每個將要添加的單個遷移壓縮100個消息。有沒有辦法將resx的xml文件或單個resx條目標記爲// < auto-generated/>,這樣就會停止發生?如果我必須禁用這兩條規則,那麼只是希望這不是唯一的理智答案!

TIA 傑森

回答

4

我這個今天才注意到,自己。您可以抑制在資源級別的CA1701和CA1703警告與或者您的的AssemblyInfo.cs或GlobalSuppressions.cs文件執行以下操作:

[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")] 

你需要爲每個遷移的做到這一點,但它的比單獨壓制每個警告要好得多。

+1

大答案......得到這個工作的最簡單方法是 - 抑制現有的警告變爲通過代碼分析窗口globalsuppressions文件之一。 - 將答案中的代碼塊複製到globalsuppressions文件中 - 將目標屬性值從抑制消息替換爲上述代碼塊的目標屬性。 – Werewolf

6

我個人厭倦了手動讓我的鎮壓最新(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")] 
+0

如果您移動遷移Configuration.cs文件,代碼分析將根據該文件的位置發出違規規則,而不是實際遷移(和T4模板)所在的位置。爲了解決這個問題,我必須使用命名空間爲我的移動(和重命名)Configuration.cs文件硬編碼命名空間變量。只是給類似情況下的任何人提供一個供參考。 非常感謝你爲此作出的創作,應該真的被標記爲答案。 – osoviejo