INSERT語句與FOREIGN KEY約束條件 「FK_tblCompInfo_tblOSVersion」衝突。衝突發生在數據庫 「Sample」,表「dbo.tblOSVersion」,列'VerID'中。聲明已經終止 。在定製MVC4中的CRUD時獲取數據庫錯誤
{
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>
被feeded看到這個在這篇文章的第一反應:http://stackoverflow.com/questions/2965837/insert-statement-conflict-with-the-foreign-key-constraint – Aliz