2016-03-05 24 views
1

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> 
+0

被feeded看到這個在這篇文章的第一反應:http://stackoverflow.com/questions/2965837/insert-statement-conflict-with-the-foreign-key-constraint – Aliz

回答

0

由於錯誤說數據不feeded外鍵的列或數據不支持映射表。 可以有兩個選項

1.如果外鍵數據應該爲空。外鍵列應該被改變以允許null。

2.如果一對多的映射是正確的只有映射表中的數據將正確地「db.CompInfoes.Add(compinfo);

+0

一旦我用默認下拉代碼替換角碼,它可以正常工作, – intel1in

+0

如果是這樣,錯誤應該是數據綁定, end.i表示數據沒有從下拉列表傳遞到該表的外鍵 –

+0

這是什麼解決方案,我需要檢查代碼? – intel1in

相關問題