如何解决将记录插入到Mvc中具有关键关系的表中时出现的问题 [英] How Can I Solve The Problem Which Occurs When I Insert A Record Into Tables Which Are Having Key Relationship In Mvc

查看:69
本文介绍了如何解决将记录插入到Mvc中具有关键关系的表中时出现的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EntityFramework.dll中出现'System.Data.Entity.Infrastructure.DbUpdateException'类型的异常但未在用户代码中处理



其他信息:更新条目时发生错误。有关详细信息,请参阅内部异常。



这是我收到的错误..



代码是: :



An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code

Additional information: An error occurred while updating the entries. See the inner exception for details.

This is the error i receive..

code is ::

var sm = new SupplierMaster();
         //  var add = new Address();

            if (ModelState.IsValid)
            {
                sm.SupplierId = ic.SupplierId;
                sm.SupplierName = ic.SupplierName;
                sm.SupplierPhone = ic.SupplierPhone;

                sm.KeyId = ic.KeyId;
                sm.UserId = ic.UserId;
                sm.createdDate = DateTime.Now;
                sm.CreatedBy = ic.UserId;
                sm.UpdatedBy = ic.UserId;
                sm.UpdatedDate = DateTime.Now;
                sm.Address1= ic.Address1;
                sm.AddType = ic.AddType;
                sm.City = ic.City;
                sm.State = ic.State;
                sm.Zip = ic.Zip;

                be.SupplierMasters.Add(sm);
                be.SaveChanges();

                ViewBag.Address = new SelectList(be.Addresses, "Address", "AddType", sm.Address);
                ViewBag.Address = new SelectList(be.Addresses, "Address", "Address1", sm.Address);
                ViewBag.Address = new SelectList(be.Addresses, "Address", "State", sm.Address);
                ViewBag.Address = new SelectList(be.Addresses, "Address", "City", sm.Address);
                ViewBag.KeyId = new SelectList(be.Addresses, "KeyId", "Address", sm.KeyId);
                ViewBag.UserId = new SelectList(be.LoginUsers, "UserId", "UserName", sm.UserId);
                ViewBag.CreatedBy = new SelectList(be.LoginUsers, "UserId", "UserName", sm.CreatedBy);
                ViewBag.UpdatedBy = new SelectList(be.LoginUsers, "UserId", "UserName", sm.UpdatedBy);
                return RedirectToAction("SupplierMaster", "Supplier");

推荐答案

根据我的理解......检查以下内容



1.您是否为您创建的表设置了任何主键,如果是这样设置标识列。



2.完成后,我们需要在数据库更改后刷新实体框架模型





希望这个解决方案有效。
As Per my understanding..check the below things

1. Have you set any primary key for the table which you created, if so set identity column.

2. Once its done, we need to Refresh Entity Framework Model After Database Change


Hope this solution works.


这篇关于如何解决将记录插入到Mvc中具有关键关系的表中时出现的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆