ADO.NET实体数据模型自动生成的代码违反了代码分析CA2214。

3
在使用VS 2012下的MVC 3解决方案时,运行代码分析后收到“DON'T”分析的消息似乎有些奇怪。这是因为提到“DON'T”分析中的类是由VS自动生成的(而非我)。要重现这个问题,您可以:1.将ADO.NET实体数据模型添加到解决方案中;2.在图表中添加数据库表(已存在于SQL Server中);3.重新构建解决方案,并添加控制器和cshtml视图(这些是自动生成的);4.从ANALYZE菜单运行代码分析。那么问题来了,我们该做什么:重构自动生成的代码还是忽略这个消息?以下是一个违反规则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...
3个回答

2

1

您可以随意忽略这些内容。请查看Entity Framework CodePlex网站上的讨论以获取更多详细信息。


1
这是出于设计考虑,以便进行懒加载。当您通过 DbContext 访问类时,尽管会收到警告,但不会在运行时失败。您应该仅在那些文件中抑制警告。

当抑制警告不是一个选项时,你该怎么做呢?我的公司不允许 FxCop 中出现任何重要的警告。 - Rochelle C
@chill182 你应该从 FxCop / StyleCop 中排除自动生成的代码。 - Monstieur

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接