开发者

Error in Creating a record in the table

Am creating a table with sql server 2008 and using linq to sql. Am using asp.net mvc3 with razor view.

while trying to create a new record am getting the following exception

Violation of PRIMARY KEY constraint 'PK_Department'. Cannot insert duplicate key in object 'dbo.Department'.

Am trying to insert record into a table which 开发者_开发问答has got it's id columns as foreign key in another table. I can't understand what error this is. Could someone help me?

The code in Department controller

// GET: /Department/Create

    public ActionResult Create()
    {
        Department department = new Department();
        return View(department);
    } 

    //
    // POST: /Department/Create

    [HttpPost]
    public ActionResult Create(FormCollection collection)
    {
        Department department = new Department();
        try
        {
            // TODO: Add insert logic here
            UpdateModel(department);//Updates all the fields in the table
            departmentRepository.Add(department);//Inserts the record
            departmentRepository.Save();//Saves all the fields in the record
            return RedirectToAction("Index");
        }
        catch(Exception ex)
        {
            var Ex_mesg = ex.Message;
            return View("Error");
        }
    }

The code in department repository

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VisitorManagementSystem.Models
{
    public class DepartmentRepository
    {  
        private VisitorManagementDataContext db = new VisitorManagementDataContext();

        //QUERYABLE METHODS
        public IQueryable<Department> FindAllDepartment()
        {
            return db.Departments;//returns all the departments in the table
        }

        //ADD, SAVE, DELETE Methods
        public void Add(Department department)
        {
            db.Departments.InsertOnSubmit(department);//Adds a record to the table
        }

        public void Save()
        {
            db.SubmitChanges();//Saves the record to the table
        }

        public void Delete(Department department)
        {
            db.Departments.DeleteOnSubmit(department);//Deletes the record on submit.
        }


    }
}

The code in razor view

@model VisitorManagementSystem.Models.Department

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">              </script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"type="text/javascript">                                                   
</script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
    <legend>Department</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
  </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>


Sounds like you are trying to insert a record into the Department table that has the same key as another existing record. Without knowing more about your schema, the existing data, or your code though I don't think I can offer a much better explanation than that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜