0

in my Entity Framework model, I have that column

    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
    [DataMemberAttribute()]
    public Nullable<global::System.Boolean> CanShow
    {
        get
        {
            return _CanShow;
        }
        set
        {
            OnCanShowChanging(value);
            ReportPropertyChanging("CanShow");
            _CanShow = StructuralObject.SetValidValue(value);
            ReportPropertyChanged("CanShow");
            OnCanShowChanged();
        }
    }
    private Nullable<global::System.Boolean> _CanShow;
    partial void OnCanShowChanging(Nullable<global::System.Boolean> value);
    partial void OnCanShowChanged();

by using the partial class, I want to add some business logic in OnCanShowChanging method

to do so, I try the code:

public partial class MyTable
{
    partial void OnCanShowChanging(bool? value)
    {
        if (some_condition)
        {
            this.CanShow = value;
        }
        //else -> avoid column data change
    }
}

But I get the StackOverflowException

I'm new in that scenario (partial method with Entity Framework), how to fix it ?

Tony
  • 12,405
  • 36
  • 126
  • 226

3 Answers3

1

You can't set CanShow from inside OnCanShowChanging.

The setter for CanShow calls OnCanShowChanging, which calls the setter again, which calls OnCanShowChanging again... And so on. Infinite loop until you overflow the stack.

TheNextman
  • 12,428
  • 2
  • 36
  • 75
1

Your code have a ciclic call. Whenever you assign a value to CanShow it fires the OnCanShowChanging again and again. Modify your logic and fix that recurrent call.

Oscar
  • 13,594
  • 8
  • 47
  • 75
1

OnCanShowChanging is being called recursively when CanShow is set. a better way to deal with this might be to do something like:

    public partial class MyTable
    {
        private bool blockOnCanShowChanging = false;

        partial void OnCanShowChanging(bool? value)
        {
            if (blockOnCanShowChanging)
            {
                return; //Recursive call...just return
            }

            if (some_condition)
            {
                //Turn on bool to avoid recusive call
                blockOnCanShowChanging = true;

                this.CanShow = value;

                //reset bool to allow subsequent calls to OnCanShowChanging
                blockOnCanShowChanging = false;
            }

            //else -> avoid column data change
        }
    }
jstromwick
  • 1,206
  • 13
  • 22