Answered by:
Entity Framework - System.StackOverflowException

Question
-
Hello all,
I am new to entity framework and have hit an issue trying to insert new items into a lookup table.
The error is - "An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll"
It is thrown in the final code block below where public DIEMEntities() is called.
It occurs whenever I add a new item, I can update items ok.
Any help would be appreciated.
The code is -
protected void OnSave(object sender, EventArgs e) { ArrayList validationErrors = new ArrayList(); ContactTypeEO contactType = new ContactTypeEO(); if (uxID.Value.Length > 0) { //Updating contactType.Id = int.Parse(uxID.Value); } contactType.Name = uxName.Text; contactType.ExpressionValidator = uxExpression.Text; contactType.Save(ref validationErrors); if (validationErrors.Count > 0) { ShowValidationMessages(validationErrors); } else { this.RefreshUI(); } }
public bool Save(ref ArrayList validationErrors) { ValidateSave(ref validationErrors); if (validationErrors.Count == 0) { if (Id == 0) { ContactTypeData.Insert(Name, ExpressionValidator); } else { ContactTypeData.Update(Id, Name, ExpressionValidator); } return true; } else { return false; } }
/// <summary> /// Inserts the new Contact Type. /// </summary> /// <param name="name">The name.</param> /// <param name="validator">The validator.</param> public static void Insert(string name, string validator) { using (DIEMEntities diemEntities = new DIEMEntities()) { Insert(name, validator); } } /// <summary> /// Inserts the new Contact Type. /// </summary> /// <param name="diemEntities">The DIEM Entities.</param> /// <param name="name">The name.</param> /// <param name="validator">The validator.</param> public static void Insert(DIEMEntities diemEntities, string name, string validator) { diemEntities.usp_ContactTypes_Insert(name, validator); }
public partial class DIEMEntities : DbContext { public DIEMEntities() : base("name=DIEMEntities") { } ... OTHER CODE }
Friday, January 4, 2013 12:23 PM
Answers
-
Hi,
Usually stack overflow means the stack is "full" for example when calling a procedure directly or indirectly from itself resulting in "infinite" nested calls filling up the whole stack. Generally speaking just check the "call stack" window in VS when the exception happens to see what causes those infinite calls...
Here it could be that in your Insert method you call again the same Insert method while you likely want to call a method belonging to diemEntities and that has the same name ?...
Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
- Edited by Patrice ScribeMVP Saturday, January 5, 2013 2:25 PM
- Proposed as answer by Alexander Sun Tuesday, January 8, 2013 1:52 AM
- Marked as answer by Alexander Sun Wednesday, January 16, 2013 9:36 AM
Saturday, January 5, 2013 2:23 PM
All replies
-
Just to add -
I have also tried with the Contact_Types procedure returning an ID, still the same error.
Friday, January 4, 2013 12:34 PM -
Hi,
Usually stack overflow means the stack is "full" for example when calling a procedure directly or indirectly from itself resulting in "infinite" nested calls filling up the whole stack. Generally speaking just check the "call stack" window in VS when the exception happens to see what causes those infinite calls...
Here it could be that in your Insert method you call again the same Insert method while you likely want to call a method belonging to diemEntities and that has the same name ?...
Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
- Edited by Patrice ScribeMVP Saturday, January 5, 2013 2:25 PM
- Proposed as answer by Alexander Sun Tuesday, January 8, 2013 1:52 AM
- Marked as answer by Alexander Sun Wednesday, January 16, 2013 9:36 AM
Saturday, January 5, 2013 2:23 PM