Reputation: 12705
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 ?
Upvotes: 0
Views: 813
Reputation: 1206
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
}
}
Upvotes: 1
Reputation: 13980
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.
Upvotes: 1
Reputation: 12566
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.
Upvotes: 1