实体框架 - System.StackOverflowException

时间:2013-01-04 13:04:55

标签: c# asp.net entity-framework stack-overflow

我是实体框架的新手,并试图将新项目插入查找表中。

错误是 -

  

System.StackOverflowException类型的未处理异常   发生在mscorlib.dll“

在下面的最终代码块中抛出它,其中公共DIEMEntities()被调用。

只要我添加新项目就会发生,我可以更新项目。

任何帮助都将不胜感激。

代码是 -

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 个答案:

答案 0 :(得分:13)

这里有一个递归循环:

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

这将持续执行,直到内存不足为止。

我相信你的意图是这样做:

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