3

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}
2
  • It is thrown in the final code block below where public DIEMEntities() is called. and you chose to provide all the code except that one? That doesn't even make sense. We have everything except the method that's failing. Commented Jan 4, 2013 at 13:06
  • Have you tried stepping through to see which line it fails at? Commented Jan 4, 2013 at 13:06

1 Answer 1

16

You have a recursive loop here:

public static void Insert(string name, string validator)
{
    using (DIEMEntities diemEntities = new DIEMEntities())
    {
        Insert(name, validator);
    }
}

This will execute continuously until you run out of memory.

I believe your intention is to do this:

public static void Insert(string name, string validator)
{
    using (DIEMEntities diemEntities = new DIEMEntities())
    {
        Insert(diemEntities, name, validator);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

plus for the term recursive loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.