在 MVC4 中自定义 CRUD 时出现数据库错误

本文关键字:数据库 错误 CRUD MVC4 自定义 | 更新日期: 2023-09-27 18:37:25

插入语句与外键约束冲突 "FK_tblCompInfo_tblOSVersion"。数据库中发生冲突 "Sample",表"dbo.tblOSVersion",列"VerID"。该声明有 已终止。

{
   db.CompInfoes.Add(compinfo);      
   db.SaveChanges();
   return RedirectToAction("Index");   
}

尝试使用以下代码自定义Create.cshtml

@model MvcApplication1.Models.CompInfo  
@{
    ViewBag.Title = "Create";
}
<script src="~/Scripts/angular.min.js"></script>
<script src="~/Scripts/MyApp.js"></script>
<script src="~/Scripts/DropDownController.js"></script>
<h2>Create</h2>
<div style="font-family:Arial">
@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>CompInfo</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.EmpName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.EmpName)
            @Html.ValidationMessageFor(model => model.EmpName)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.ComputerName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ComputerName)
            @Html.ValidationMessageFor(model => model.ComputerName)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.ServiceTag)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ServiceTag)
            @Html.ValidationMessageFor(model => model.ServiceTag)
        </div>
        <div class="editor-label">
        <div ng-app="MyApp" ng-controller="DropDownController">
                Country : <select ng-model="OSID" ng-options="I.OSID as I.OSName for I in OSList" ng-change="GetVersion()">
                <option value="">Select OS</option>
                </select><br />
                State : <select ng-model="VerID" ng-options="I.VerID as I.VerName for I in VersionList">
                <option value="">{{VersionTextToShow}}</option>
            </select>
    </div>
</div>
        @*<div class="editor-label">
            @Html.LabelFor(model => model.OSID, "OperatingSystem")
        </div>
        <div class="editor-field">
            @Html.DropDownList("OSID", String.Empty)
            @Html.ValidationMessageFor(model => model.OSID)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.VerID, "OSVersion")
        </div>
        <div class="editor-field">
            @Html.DropDownList("VerID", String.Empty)
            @Html.ValidationMessageFor(model => model.VerID)
        </div>*@
        <div class="editor-label">
            @Html.LabelFor(model => model.IP)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.IP)
            @Html.ValidationMessageFor(model => model.IP)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.EmailID)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.EmailID)
            @Html.ValidationMessageFor(model => model.EmailID)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.SysTypeID, "SystemType")
        </div>
        <div class="editor-field">
            @Html.DropDownList("SysTypeID", String.Empty)
            @Html.ValidationMessageFor(model => model.SysTypeID)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.CStatusID, "StatusComputer")
        </div>
        <div class="editor-field">
            @Html.DropDownList("CStatusID", String.Empty)
            @Html.ValidationMessageFor(model => model.CStatusID)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
</div>

在 MVC4 中自定义 CRUD 时出现数据库错误

截至错误指出数据未馈送到外键列或不支持映射到映射表的数据。可以有两种选择

1.如果外键的数据应为空。应更改外键列以允许 null。

2.如果一对多映射正确,则只有映射表数据将正确馈送"db.CompInfoes.Add(compinfo);"