c# - 坐在窗口asp.net mvc时添加实体后发生StackOverflowException

标签 c# asp.net asp.net-mvc entity-framework

我正在编写一个应用程序,允许 DBA 从数据库中查看/添加/编辑/删除服务器信息。我是 asp.net mvc 的新手,但我正在尝试遵循最佳实践。

我首先启动了数据库,然后使用 Visual Studio 生成了一个模型。然后我再次使用 visual studio 基于这个模型生成了一个 View 。一切都很好,它显示了我想要的样子。当我尝试添加一个实体时,奇怪的事情就来了。当我添加一个实体并将其保存在数据库中时,它会正确返回并在列表中显示新服务器。大约 5-10 秒后,应用程序崩溃并出现 System.StackOverflowException。如果我关闭应用程序并重新打开它并再次导航到服务器 View ,它会在 5-10 秒后再次崩溃。

我单步执行了添加实体的代码,一切都已正确设置,添加了实体并保存了数据库。

如果我手动从数据库中删除这个实体,然后再次加载应用程序——什么都没有。工作得很好。

这是我添加服务器的代码:

    [HttpPost]
    public ActionResult Create(FormCollection collection)
    {
        string serverAlias = collection["ServerAlias"];
        string domain = collection["Domain"];
        string instanceName = collection["InstanceName"];
        string location = collection["Location"];
        string edition = collection["Edition"];
        Nullable<System.DateTime> lastUpdated = null;


        SQLServer toAdd = new SQLServer()
        {
           ServerAlias = serverAlias,
           Domain = domain,
           InstanceName = instanceName,
           Location = Convert.ToByte(location),
           LastUpdated = lastUpdated,
           SQLServerEdition = Convert.ToByte(edition)
        };
        try
        {
            if (ModelState.IsValid)
            {
                _ServerDB.SQLServers.Add(toAdd);
                _ServerDB.SaveChanges();
            }
            return RedirectToAction("Index");
        }
        catch (Exception e)
        {
            //will add more robust error handling later
            return Content("Failed to add server");
        }
    }

需要注意的是,上面的Location和SQLServerEdition是外键ID。

我已单步执行此代码并确保为每个值设置正确的值。在 StackOverflowException 爆炸之前,服务器列表再次显示的前几分钟确认了这一点。

下面是SQLServer模型的定义。

public partial class SQLServer
{
    public short ServerID { get; set; }
    [Required, Display(Name="Server Alias")]
    public string ServerAlias { get; set; }
    [Required]
    public string Domain { get; set; }

    public byte SQLServerEdition { get; set; }
    [Required]
    public string InstanceName { get; set; }
    public Nullable<byte> Location { get; set; }

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
    public Nullable<System.DateTime> LastUpdated { get; set; }

    public virtual Edition Edition { get; set; }
    public virtual ServerLocation ServerLocation { get; set; }
}

关于我在这里遗漏的任何想法?

编辑:这是 View 代码:

@model IEnumerable<MSSQL_Server_Viewer.Models.SQLServer>

@{
    ViewBag.Title = "Server Index";
}
<br />
<br />

<label>@ViewBag.Title</label>

<p>
@Html.ActionLink("Add New Server", "Create")
</p>
<table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.ServerAlias)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.InstanceName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Domain)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Edition.Edition1)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.ServerLocation.Location)
    </th>
    <th></th>
</tr>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(model => model.ServerAlias)
    </td>
    <td>
        @Html.DisplayFor(model => model.InstanceName)
    </td>
    <td>
        @Html.DisplayFor(model => model.Domain)
    </td>
    <td>
        @Html.DisplayFor(model => model.Edition.Edition1)
    </td>
    <td>
        @Html.DisplayFor(model => model.ServerLocation.Location)
    </td>
    <td>
        <table border="0">
            <tr>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.ServerID })
                </td>
                <td> | </td>
                <td>
                    @Html.ActionLink("Details", "Details", new { id = item.ServerID })
                </td>
                <td> | </td>
                <td>
                    @Html.ActionLink("Delete", "Delete", new { id = item.ServerID })
                </td>
            <tr>
        </table>
    </td>
</tr>
}

</table>

最佳答案

对于以后搜索此内容的任何人 - 我找到了解决方案。这不是我预期的那样/在哪里,而且是这么小的错误。

在下面的代码中:

<tr>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id = item.ServerID })
    </td>
    <td> | </td>
    <td>
        @Html.ActionLink("Details", "Details", new { id = item.ServerID })
    </td>
    <td> | </td>
    <td>
         @Html.ActionLink("Delete", "Delete", new { id = item.ServerID })
    </td>
<tr>

最后一个“tr”是一个结束的“/tr”

我不太熟悉 HTML,所以我不完全确定为什么这不只是抛出语法错误。这么小的错误,让我伤心了好几天。我希望下一个偶然发现这个问题的人比我更快地找到问题。

关于c# - 坐在窗口asp.net mvc时添加实体后发生StackOverflowException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35467682/

相关文章:

c# - 抛出异常是一个好习惯吗?

c# - ASP.NET MVC3 角色

c# - 在 ActionFilterAttribute.OnActionExecuted 中设置 Layout 是有问题的

asp.net-mvc - 服务器使用标准 asp.net mvc 发送事件(事件源)导致错误

c# - 无法在 Framework 4.6.1 中创建 Specflow 文件

javascript - 有没有办法通过 javascript/jquery 将 Mongo ObjectId 转换为字符串

c# - 在 C# 中使用自定义构造函数创建异常

java - 从 xamarin java 绑定(bind)中删除 virtual 关键字

javascript - Asp.net MVC使用ajax更新网页15秒后无需重新加载页面

asp.net-mvc - 使用 Angular 创建和更新实体的好方法是什么?