DBUpdate异常未处理

时间:2015-11-17 13:48:06

标签: c# asp.net asp.net-mvc-4

我试图插入包含来自[tbl.One]和[tbl.Two]的外键的[tbl.three]。我已经使用[tbl.One]中的主键和MVC 4中的[tbl.Two]填充了下拉列表。它显示在下拉列表中  但是当我点击提交时,它会显示以下错误。

请帮忙,谢谢您的时间

这是我的数据库,包含给定的表格:

Create database [ForeignKey]

Create table[tbl.One]
(
   [Occupation] Varchar(20) Primary Key,
   [Area] Varchar(20)
)

insert into [tbl.One] values('IT', 'India')
insert into [tbl.One] values('MCSD', 'China')
insert into [tbl.One] values('MCSE', 'Maimi')
insert into [tbl.One] values('Cisco', 'Hawaii')

Create table[tbl.Two]
(
   [Address] Varchar(20) Primary Key,
   [Surburb] Varchar(20)
)

insert into [tbl.Two] values('18 Jet Park','Florida')
insert into [tbl.Two] values('25 High Road','Hamberg')
insert into [tbl.Two] values('1 Main Reef','Discovery')
insert into [tbl.Two] values('3 Kink Road','Constancia')

Create table[tbl.Three]
(
   [id] int Primary key,
   [Name] Varchar(20),
   [Surname] Varchar(20),
   [Occupation] Varchar(20) foreign key references [tbl.One]([Occupation]),
   [Occupation1] Varchar(20) foreign key references [tbl.One]([Occupation]),
   [Address] Varchar(20) foreign key references [tbl.Two]([Address]),
)

这是我的错误:

enter image description here

这是我的控制器代码:

 public class HomeController : Controller
    {
        private ForeignKeyEntities db = new ForeignKeyEntities();

        //
        // GET: /Home/

        public ActionResult Index()
        {
            var tbl_three = db.tbl_Three.Include(t => t.tbl_One).Include(t => t.tbl_One1).Include(t => t.tbl_Two);
            return View(tbl_three.ToList());
        }

        //
        // GET: /Home/Details/5

        public ActionResult Details(int id = 0)
        {
            tbl_Three tbl_three = db.tbl_Three.Find(id);
            if (tbl_three == null)
            {
                return HttpNotFound();
            }
            return View(tbl_three);
        }

        //
        // GET: /Home/Create

        public ActionResult Create()
        {
            ViewBag.Occupation = new SelectList(db.tbl_One, "Occupation", "Area");
            ViewBag.Occupation1 = new SelectList(db.tbl_One, "Occupation", "Area");
            ViewBag.Address = new SelectList(db.tbl_Two, "Address", "Surburb");
            return View();
        }

        //
        // POST: /Home/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(tbl_Three tbl_three)
        {
            if (ModelState.IsValid)
            {
                db.tbl_Three.Add(tbl_three);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.Occupation = new SelectList(db.tbl_One, "Occupation", "Area", tbl_three.Occupation);
            ViewBag.Occupation1 = new SelectList(db.tbl_One, "Occupation", "Area", tbl_three.Occupation1);
            ViewBag.Address = new SelectList(db.tbl_Two, "Address", "Surburb", tbl_three.Address);
            return View(tbl_three);
        }

        //
        // GET: /Home/Edit/5

        public ActionResult Edit(int id = 0)
        {
            tbl_Three tbl_three = db.tbl_Three.Find(id);
            if (tbl_three == null)
            {
                return HttpNotFound();
            }
            ViewBag.Occupation = new SelectList(db.tbl_One, "Occupation", "Area", tbl_three.Occupation);
            ViewBag.Occupation1 = new SelectList(db.tbl_One, "Occupation", "Area", tbl_three.Occupation1);
            ViewBag.Address = new SelectList(db.tbl_Two, "Address", "Surburb", tbl_three.Address);
            return View(tbl_three);
        }

        //
        // POST: /Home/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(tbl_Three tbl_three)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tbl_three).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.Occupation = new SelectList(db.tbl_One, "Occupation", "Area", tbl_three.Occupation);
            ViewBag.Occupation1 = new SelectList(db.tbl_One, "Occupation", "Area", tbl_three.Occupation1);
            ViewBag.Address = new SelectList(db.tbl_Two, "Address", "Surburb", tbl_three.Address);
            return View(tbl_three);
        }

        //
        // GET: /Home/Delete/5

        public ActionResult Delete(int id = 0)
        {
            tbl_Three tbl_three = db.tbl_Three.Find(id);
            if (tbl_three == null)
            {
                return HttpNotFound();
            }
            return View(tbl_three);
        }

        //
        // POST: /Home/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            tbl_Three tbl_three = db.tbl_Three.Find(id);
            db.tbl_Three.Remove(tbl_three);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}

这是我的查看(创建)代码:

<fieldset>
    <legend>tbl_Three</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>

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

    <div class="editor-label">
        @Html.LabelFor(model => model.Occupation, "tbl_One")
    </div>
    <div class="editor-field">
        @Html.DropDownList("Occupation", String.Empty)
        @Html.ValidationMessageFor(model => model.Occupation)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Occupation1, "tbl_One1")
    </div>
    <div class="editor-field">
        @Html.DropDownList("Occupation1", String.Empty)
        @Html.ValidationMessageFor(model => model.Occupation1)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Address, "tbl_Two")
    </div>
    <div class="editor-field">
        @Html.DropDownList("Address", String.Empty)
        @Html.ValidationMessageFor(model => model.Address)
    </div>

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

}

2 个答案:

答案 0 :(得分:1)

这一行

db.tbl_Three.Add(tbl_three);

将项目tbl_three添加到数据库tbl_Three,该数据库已包含具有tbl_three主键的项目(不允许)。

问题出现是因为您的Create视图未获得 new tbl_three,而是现有视图。您可以使用原始属性创建新的tbl_three,然后Add - 这将确保它具有唯一的主键。

答案 1 :(得分:1)

Id列是主键,看起来您正在向表中插入重复的Id值以获取多个记录。

您应该做的是将表创建更改为使用Identity作为主键列。这样,您不需要插入Id列的值。每次添加新记录时,SQL Server都会自动添加新的ID值

Create table[tbl.Three]
(
   [id] int Primary key  IDENTITY(1,1) NOT NULL,
   [Name] Varchar(20),
   [Surname] Varchar(20),
   [Occupation] Varchar(20) foreign key references [tbl.One]([Occupation]),
   [Occupation1] Varchar(20) foreign key references [tbl.One]([Occupation]),
   [Address] Varchar(20) foreign key references [tbl.Two]([Address]),
)