Wer*_*olf 4 code-analysis resx ef-migrations entity-framework-5
在为我的代码优先EF 5上下文启用迁移后,由于迁移历史记录字符串被添加到项目的resx文件,我开始收到TON的CA1701和CA1703代码分析违规.
我不关心禁用CA1701和CA1703,也不想为每个要添加的迁移抑制100条消息.有没有办法将resx的xml文件或单个resx条目标记为// <自动生成/>以便停止发生?如果我必须禁用这两个规则,那么它只是希望这不是唯一合理的答案!
TIA杰森
我个人厌倦了手动保持我的抑制最新(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)
我今天才注意到这一点,我自己.您可以使用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)
您需要为每次迁移执行此操作,但这比单独抑制每个警告要好得多.
归档时间: |
|
查看次数: |
881 次 |
最近记录: |