1

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tblCompInfo_tblOSVersion". The conflict occurred in database "Sample", table "dbo.tblOSVersion", column 'VerID'. The statement has been terminated.

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

Tried to customize Create.cshtml with the following code:

@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>
TotPeRo
  • 6,561
  • 4
  • 47
  • 60
intel1in
  • 61
  • 7
  • See this the first response in this post: http://stackoverflow.com/questions/2965837/insert-statement-conflicted-with-the-foreign-key-constraint – Aliz Mar 05 '16 at 11:39

1 Answers1

0

As of the error saying the data is not feeded to foreign key column or data is not supported to the mapping table. There can be two options

1.if the data for foreign key should be null. The foreign key column should be alterted to allow null.

2.if the one to many mapping is correct only the mapping table data will be feeded correctly with " db.CompInfoes.Add(compinfo); "

VVN
  • 1,607
  • 2
  • 16
  • 25