Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ADO.NET Entity Data Model auto generated code violates Code Analysis CA2214

In a MVC 3 solution, under VS 2012, it sounds strange that you receive such message after Running Code Analysis:

enter image description here

It is strange because the class which is mentioned in the "DON'T" analysis was generated by VS itself (not me).

To reproduce this you can:

  1. add ADO.NET Entity Data Model to your solution,
  2. add your database tables (already existent in SQL Server) to your diagram,
  3. rebuild solution, add Controllers and cshtml views (those ones are generated automatically).
  4. run the code analysis from ANALYZE menu.

So, the question is, what could we do: Refactor the auto-generated code or ignore this message?

Below a sample of auto generated class which violates the rule 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...
like image 846
Junior Mayhé Avatar asked Aug 16 '26 11:08

Junior Mayhé


2 Answers

Here's how I solved it permanently: http://chuckbeasley.com/blog/?p=42200

like image 60
chuckbeasley Avatar answered Aug 18 '26 03:08

chuckbeasley


This is by design to allow for lazy loading. When you access the classs through DbContext it won't fail at run-time despite the warning. You should suppress the warnings only in those files.

like image 27
Monstieur Avatar answered Aug 18 '26 04:08

Monstieur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!