Entity framework ADO.NET实体数据模型自动生成的代码违反代码分析CA2214
在MVC 3解决方案中,在VS 2012下,运行代码分析后收到这样的消息听起来很奇怪: 这很奇怪,因为在“DON'T”分析中提到的类是由VS本身(不是我)生成的 要重现此情况,您可以:Entity framework ADO.NET实体数据模型自动生成的代码违反代码分析CA2214,entity-framework,ado.net,visual-studio-2012,code-analysis,Entity Framework,Ado.net,Visual Studio 2012,Code Analysis,在MVC 3解决方案中,在VS 2012下,运行代码分析后收到这样的消息听起来很奇怪: 这很奇怪,因为在“DON'T”分析中提到的类是由VS本身(不是我)生成的 要重现此情况,您可以: 将ADO.NET实体数据模型添加到解决方案中 将数据库表(已存在于SQL Server中)添加到图表中 重建解决方案,添加控制器和cshtml视图(这些视图是自动生成的) 从“分析”菜单运行代码分析 所以,问题是,我们能做什么:重构自动生成的代码还是忽略这个消息 下面是违反规则CA2214的自动生成类示例: /
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Backoffice.Entities
{
using System;
using System.Collections.Generic;
public partial class BR_SIMUL_Supermarket_Product
{
public BR_SIMUL_Supermarket_Product()
{
this.BR_SIMUL_Supermarket_Product_Price = new HashSet<BR_SIMUL_Supermarket_Product_Price>();
}
public int product_id { get; set; }
public int category_id { get; set; }
public string product_name { get; set; }
public string product_measure { get; set; }
public bool product_active { get; set; }
public virtual BR_SIMUL_Supermarket_Category BR_SIMUL_Supermarket_Category { get; set; }
more stuff here...
//------------------------------------------------------------------------------
//
//此代码是从模板生成的。
//
//手动更改此文件可能会导致应用程序出现意外行为。
//如果重新生成代码,将覆盖对此文件的手动更改。
//
//------------------------------------------------------------------------------
名称空间Backoffice.Entities
{
使用制度;
使用System.Collections.Generic;
公共部分类BR_SIMUL_超市产品
{
公共模拟超市产品()
{
this.BR_SIMUL_Supermarket_Product_Price=new HashSet();
}
公共int乘积_id{get;set;}
公共int类_id{get;set;}
公共字符串产品名称{get;set;}
公共字符串乘积_度量值{get;set;}
公共布尔乘积_活动{get;set;}
公共虚拟虚拟虚拟超市类别虚拟超市类别{get;set;}
这里有更多的东西。。。
您可以随意抑制这些。有关更多详细信息,请查看Entity Framework codeplex网站上的这一点。这是为了允许延迟加载而设计的。当您通过DbContext
访问类时,尽管有警告,但它在运行时不会失败。您应该只抑制那些文件中的警告。以下是我是如何永久解决的:当禁止警告不是一个选项时,您会怎么做?我的公司不允许在FxCop中出现任何严重警告。@您应该从FxCop/StyleCop中排除自动生成的代码。