Entity Framework - System.StackOverflowException

asp.net c# entity-framework stack-overflow

Question

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}
1
2
1/20/2013 5:55:01 PM

Accepted Answer

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 you're intention is to do this:

public static void Insert(string name, string validator)
{
    using (DIEMEntities diemEntities = new DIEMEntities())
    {
        Insert(diemEntities, name, validator);
    }
}
15
1/4/2013 1:07:06 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow