2

I'm working on a project where a record needs to be inserted, using a context class. However, before the insertion, one of the properties 'DataArea' is being set to a value that it should not have.

I started debugging the code to see where exactly this value is being set, and I have altered the short written version of the getter and setter (get; set;), to this:

public DataAreaEnum _DataArea;
public DataAreaEnum DataArea
    {
        get
        {
            return _DataArea;
        }
        set
        {
            _DataArea = value;
            Debugger.Break();
        }
    }

By doing this, I had hoped that the debugger would break when the value was being set, however this is never hit. I am stuck and do not know how to think about this. Are there any other ways this value could have been set? How can I found out where this value was being set, if the breakpoint in the setter is never hit?

The complete class code:

using Works.Common.Models;
using System.Collections.Generic;
using System.Diagnostics;

namespace Works.Models.Toolsheets
{
    public partial class Toolblad : ModelBase
    {
        public Toolblad()
        {
            Bijlagen = new HashSet<Bijlage>();
            //Carrousels = new HashSet<Carrousel>();
            KlauwplaatSlijpers = new HashSet<KlauwplaatSlijper>();
            Klauwplaten = new HashSet<Klauwplaat>();
            Laadtafels = new HashSet<Laadtafel>();
            Laders = new HashSet<Lader>();
            MblStangenLaders = new HashSet<MblStangenLader>();
            MblStangenLaderWisselpunten = new HashSet<MblStangenLaderWisselpunt>();
            Shuttles = new HashSet<Shuttle>();
            Spankoppen = new HashSet<Spankop>();
            SpankopSlijpers = new HashSet<SpankopSlijper>();
            Revolvers = new HashSet<ToolbladRevolver>();
        }

        public DataAreaEnum _DataArea;
        public DataAreaEnum DataArea
        {
            get
            {
                return _DataArea;
            }
            set
            {
                _DataArea = value;
                Debugger.Break();
            }
        }

        public bool IsTemplate { get; set; }

        #region Navigation Properties

        public virtual AlgemeneInfo AlgemeneInfo { get; set; }

        public int AlgemeneInfoId { get; set; }

        public ICollection<Bijlage> Bijlagen { get; set; }

        //public ICollection<Carrousel> Carrousels { get; set; }

        public ICollection<KlauwplaatSlijper> KlauwplaatSlijpers { get; set; }

        public ICollection<Klauwplaat> Klauwplaten { get; set; }

        public ICollection<Laadtafel> Laadtafels { get; set; }

        public ICollection<Lader> Laders { get; set; }

        public MachineGroep MachineGroep { get; set; }

        public int MachineGroepId { get; set; }

        public ICollection<MblStangenLader> MblStangenLaders { get; set; }

        public ICollection<MblStangenLaderWisselpunt> MblStangenLaderWisselpunten { get; set; }

        public ICollection<ToolbladRevolver> Revolvers { get; set; }

        public ICollection<Shuttle> Shuttles { get; set; }

        public ICollection<Spankop> Spankoppen { get; set; }

        public ICollection<SpankopSlijper> SpankopSlijpers { get; set; }

        #endregion Navigation Properties
    }
}
  • 2
    `public DataAreaEnum _DataArea;` That should be private. – mjwills Jan 19 '21 at 13:33
  • After setting this to private, the issue persists. The value is still being set, it seems. – Stefan Eeckhoudt Jan 19 '21 at 13:48
  • 3
    I note that Toolblad is a partial class. Is there any code elsewhere changing `_DataArea`? Try renaming it to something else (e.g. `_dataArea`) just in that code file and see if it all still compiles. – Matthew Watson Jan 19 '21 at 13:51
  • Thank you for the tip @MatthewWatson, all still compiles after this, tough. – Stefan Eeckhoudt Jan 19 '21 at 13:56
  • I suspect the backing field is updated somewhere, your setter would not fire in that case. Remove the backing field _DataArea alltogether, and make DataArea a normal property.. compile and see what errors you get. When no errors occur I don't know what is happening.. – Goodies Jan 19 '21 at 13:56
  • @Goodies before I started debugging, there was no backing field. I have added one manually to add the 'Debugger.Break()' code. When removing the backing field, all stays the same. – Stefan Eeckhoudt Jan 19 '21 at 13:57
  • Have you tried Debug.WriteLine("Hello") instead of Debugger.Break ? See also answers and comments about using Debugger.Break here https://stackoverflow.com/questions/104235/how-can-i-use-debugbreak-in-c – Goodies Jan 19 '21 at 13:59
  • 3
    What is the value being set to? If it is an enum, it would default to 0. [See here for details](https://stackoverflow.com/a/4967673/1390548) – Jawad Jan 19 '21 at 14:02
  • Just guessing: do you create multiple instances of this class? – Klaus Gütter Jan 19 '21 at 14:07
  • @Goodies I've tried that, but no luck. – Stefan Eeckhoudt Jan 19 '21 at 14:35
  • 4
    I guess that you're using `EFCore` (why not tagged it?), so looks like the property access mode is `Field` so the getter & setter of the property is bypassed completely. Try configuring for the whole type `Toolblad` like `modelBuilder.Entity().UsePropertyAccessMode(PropertyAccessMode.Property)` or for each specific property of your choice. – King King Jan 19 '21 at 14:36
  • @Jawad, It's being set to the first value in the enum indeed. This would have index 0. I will look into this, as I think this may be the answer. – Stefan Eeckhoudt Jan 19 '21 at 14:37
  • @KingKing EF Core is being used, indeed. I've added the tag. I will look into your answer. Thanks for your input. – Stefan Eeckhoudt Jan 19 '21 at 14:40
  • @MatthewWatson, could you post your comment as answer? Your tip has helped me solving the problem. – Stefan Eeckhoudt Jan 19 '21 at 15:38
  • @Jawad, could you post your comment as answer? – Stefan Eeckhoudt Jan 20 '21 at 09:04

2 Answers2

2

I note that Toolblad is a partial class. Is there any code elsewhere changing _DataArea? Try renaming it to something else (e.g. _dataArea) just in that code file and see if it all still compiles.

Doing that will catch any unexpected code that tries to access it.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
1

Whether you set the value of a Enum or not, it defaults to 0 which is why you are seeing a value for it without setting one.

Jawad
  • 11,028
  • 3
  • 24
  • 37