System.StackOverflowException 是未处理的错误

System.StackOverflowException was unhandled error

我对存储库和模拟存储库还很陌生,所以我想帮助理解这个错误的确切含义以及如何避免它。我查看了其他帖子并进行了研究,但它们对错误过于具体。 破解的代码是

public int PersistComponentDb(Core.Models.ComponentDbModel componentDb)
{
    return PersistComponentDb(componentDb);
}

我知道该函数正在调用自身,但如何阻止它调用自身?它持久化了一个模拟存储库,硬编码值如下所示

public DataTable GetComponentDbs(int? forComponentId = null, int? forDbServerId = null, int? forEntityId = null)
{
    DataTable componentDbs = new DataTable();
    componentDbs.Columns.Add("ComponentDbID", typeof(Int32));
    componentDbs.Columns.Add("ComponentID", typeof(Int32));
    componentDbs.Columns.Add("DbServerID", typeof(Int32));
    componentDbs.Columns.Add("EntityID", typeof(int));
    componentDbs.Columns.Add("SecurableGuid", typeof(String));
    componentDbs.Columns.Add("FriendlyName", typeof(String));
    componentDbs.Columns.Add("DbName", typeof(String));
    componentDbs.Rows.Add(new object[] { 1, 2, 2, 3822, "SecureableGuid", "Test #1", "DB #1" });
    return componentDbs;
}

public DataRow GetComponentDb(int id)
{
    DataTable componentDbs = new DataTable();   
    componentDbs.Columns.Add("ComponentDbID", typeof(Int32));
    componentDbs.Columns.Add("ComponentID", typeof(Int32));
    componentDbs.Columns.Add("DbServerID", typeof(Int32));
    componentDbs.Columns.Add("EntityID", typeof(Int32));
    componentDbs.Columns.Add("SecurableGuid", typeof(String));
    componentDbs.Columns.Add("FriendlyName", typeof(String));
    componentDbs.Columns.Add("DbName", typeof(String));
    componentDbs.Rows.Add(new object[] { 123, 121, 12, null, "SecurableGuid", "Name", "Database name" });
    return componentDbs.Rows[0];
}

PersistComponentDb 后面的其余代码是

public int PersistComponentDb(ComponentDbModel componentDb)
{
    // Example implementation.
    return _repository.PersistComponentDb(componentDb);
}

int PersistComponentDb(ComponentDbModel componentDb);

此方法正在调用自身。因此计算器溢出。

public int PersistComponentDb(Core.Models.ComponentDbModel componentDb)
{
    return PersistComponentDb(componentDb);
}

这个代码应该是:

public int PersistComponentDb(Core.Models.ComponentDbModel componentDb)
{
    return _repository.PersistComponentDb(componentDb);
}