3

我是实体框架的新手,在尝试将新项目插入查找表时遇到了问题。

错误是 -

System.StackOverflowException “ mscorlib.dll中出现未处理的类型异常”

它被抛出到下面DIEMEntities()调用 public 的最终代码块中。

每当我添加新项目时都会发生这种情况,我可以更新项目。

任何帮助,将不胜感激。

代码是 -

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}
4

1 回答 1

16

你在这里有一个递归循环:

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);
    }
}
于 2013-01-04T13:07:06.653 回答