MVC5存储库模式,“DbUpdateException未被用户代码处理” [英] MVC5 repository pattern, "DbUpdateException was unhandled by user code"

查看:60
本文介绍了MVC5存储库模式,“DbUpdateException未被用户代码处理”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是mvc架构的新手。我基本上都在办理贷款申请表。但是,在尝试将数据插入数据库时​​,它会在调试时中断并抛出此异常错误。任何帮助将不胜感激:)



这是我的Model类,其中加入了客户表和身份证件表。



Hi guys, I am fairly new to the mvc architecture. I'm basically doing an application form for loans. However, when attempting to insert data into the database, it breaks at debug and throws this exception error. Any help will be appreciated :)

This is my Model class in which the customer table and identity document table are joined.

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Template.Data;


namespace Template.Model
{
    public class CustomerIdentityView
    {
        //Customer details
        public int customerNumber { get; set; }

        public string firstName { get; set; }
        public string lastName { get; set; }
        public string gender { get; set; }
        public DateTime dateOfBirth { get; set; }
        public int age { get; set; }
        public string emailAddress { get; set; }
        public string businessPhoneNumber { get; set; }
        public string homePhoneNumber { get; set; }
        public string mobilePhoneNumber { get; set; }
        public int faxNumber { get; set; }
        public string address { get; set; }
        public string city { get; set; }
        public string province { get; set; }
        public int postalCode { get; set; }
        public string homeLanguage { get; set; }
        public string homeStatus { get; set; }
        public string maritalStatus { get; set; }
        public int numberOfDependents { get; set; }
        public string methodOfCommunication { get; set; }
        public int trustRating { get; set; }

        public virtual ICollection<LoanApplicationView> LoanApplications { get; set; }
        public virtual ICollection<IdentityDocumentView> IdentityDocuments { get; set; }
        public virtual ICollection<PayslipView> Payslips { get; set; }
        public virtual ICollection<BankStatementView> BankStatements { get; set; }
        public virtual ICollection<LoanPaymentView> LoanPayment { get; set; }

        //Identity Doc details
        public string idNumber { get; set; }
        public byte idDocument { get; set; }
        public bool submissionStatus { get; set; }

        public int employeeNumber { get; set; }
        [ForeignKey("employeeNumber")]
        public virtual Employee Employees { get; set; }

        public bool IDVerificationStatus { get; set; }
        public string creditCheckStatus { get; set; }
        public string DateOfVerfiication { get; set; }

        public int customerNumber { get; set; } // this is underlined in blue, why?
        [ForeignKey("customerNumber")]
        public virtual Customer Customers { get; set; }

    }
}











这是我的Homecontroller:








This is my Homecontroller:

using System.Web;
using System.Web.Mvc;
using Microsoft.Owin.Security;
using Template.BusinessLogic;
using Template.Model;
using System.IO;

namespace Template.MVC5.Controllers
{
    //[Authorize]
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        readonly EnquiriesBusiness _Enquiry = new EnquiriesBusiness();
        readonly CustomerIdentityBusiness _cib= new CustomerIdentityBusiness();
        readonly PayslipBusiness _psb = new PayslipBusiness();
        readonly BankStatementBusiness _bsb = new BankStatementBusiness();

        private IAuthenticationManager AuthenticationManager
        {
            get
            {
                return HttpContext.GetOwinContext().Authentication;
            }
        }
        public ActionResult Index()
        {
            var clinicbusiness = new ClinicBusiness();

            return View(clinicbusiness.GetAllClinics());
        }
        
        public ActionResult Home()
        {
            var clinicbusiness = new ClinicBusiness();

            return View(clinicbusiness.GetAllClinics());
        }
        public ActionResult Application()
        {
            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Application(CustomerIdentityView model)
        {
            if(ModelState.IsValid)
            {
                _cib.AddCustomers(model);
                return Redirect("~/Home/Employment");
            }
            return View(model);

        }




        
        public ActionResult Employment()
        {
            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Employment(PayslipView model)
        {
            if(ModelState.IsValid)
            {
                _psb.AddPayslips(model);
                return RedirectToAction("~/Home/Banking");
            }
            return View(model);

        }


       
        public ActionResult Banking()
        {
            return View();
        }
        
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Banking(BankStatementView model)
        {
            if(ModelState.IsValid)
            {
                _bsb.AddBankStatements(model);
                return RedirectToAction("~/Home/Submit");

            }
            return View(model);
        }


        public ActionResult Submit()
        {
            return View();


        }
}











这是我的模型的businessLogic层:CustomerIdentityView






And this is my businessLogic layer for the model: CustomerIdentityView

using System.Collections.Generic;
using System.Linq;
using Microsoft.Owin.Security;
using Template.Data;
using Template.Model;
using Template.Service;
namespace Template.BusinessLogic
{
    public class CustomerIdentityBusiness : ICustomerIdentityBusiness
    {
        public void AddCustomers(CustomerIdentityView customer)
        {
            var cust = new CustomerBusiness();
            cust.AddCustomers(new CustomerView
            {
                customerNumber=customer.customerNumber,
                firstName = customer.firstName,
                lastName = customer.lastName,
                gender = customer.gender,
                age=customer.age,
                dateOfBirth = customer.dateOfBirth,
                emailAddress = customer.emailAddress,
                businessPhoneNumber = customer.businessPhoneNumber,
                homePhoneNumber = customer.homePhoneNumber,
                mobilePhoneNumber = customer.mobilePhoneNumber,
                faxNumber = customer.faxNumber,
                address = customer.address,
                city = customer.city,
                province = customer.province,
                postalCode = customer.postalCode,
                methodOfCommunication = customer.methodOfCommunication,
                homeLanguage=customer.homeLanguage,
                homeStatus=customer.homeStatus,
                maritalStatus=customer.maritalStatus,
                numberOfDependents=customer.numberOfDependents,
                trustRating=customer.trustRating


            });

            var id = new IdentityDocumentBusiness();
            id.AddIdentityDocuments(new IdentityDocumentView
            {
                idNumber = customer.idNumber,
                idDocument = customer.idDocument,
                submissionStatus = customer.submissionStatus,
                employeeNumber = customer.employeeNumber,
                DateOfVerfiication = customer.DateOfVerfiication,
                IDVerificationStatus = customer.IDVerificationStatus,


            });


        }
        public IEnumerable<CustomerIdentityView> GetAll()
        {
            var myAppList = new List<CustomerIdentityView>();
            var customerBusiness = new CustomerBusiness();
            var identityDocument = new IdentityDocumentBusiness();

            foreach (var c in customerBusiness.GetAllCustomers())
            {
                var id = identityDocument.GetIdentityDocumentsById(c.customerNumber);

                var customerId = new CustomerIdentityView
                {

                   customerNumber=c.customerNumber,
                    firstName = c.firstName,
                    lastName = c.lastName,
                    gender = c.gender,
                    dateOfBirth = c.dateOfBirth,
                    age=c.age,
                    emailAddress = c.emailAddress,
                    businessPhoneNumber = c.businessPhoneNumber,
                    homePhoneNumber = c.homePhoneNumber,
                    mobilePhoneNumber = c.mobilePhoneNumber,
                    faxNumber = c.faxNumber,
                    address = c.address,
                    city = c.city,
                    province = c.province,
                    postalCode = c.postalCode,
                    creditCheckStatus = id.creditCheckStatus,
                    methodOfCommunication = c.methodOfCommunication,
                    idNumber = id.idNumber,
                    idDocument = id.idDocument,
                    submissionStatus = id.submissionStatus,
                    employeeNumber = id.employeeNumber,
                    DateOfVerfiication = id.DateOfVerfiication,
                    IDVerificationStatus = id.IDVerificationStatus,




                };
                myAppList.Add(customerId);
            }
            return myAppList;
        }



    }
}

推荐答案

删除...



[ForeignKey(employeeNumber)]

公共虚拟员工员工{get;组; }



并离开...

public int employeeNumber {get;组; }; //这表示模型中的外键
Hi, remove...

[ForeignKey("employeeNumber")]
public virtual Employee Employees { get; set; }

and leave...
public int employeeNumber { get; set; }; //this represents the Foreign key in the model


只是尝试用你的代码替换下面的代码:

Just try to replace the following code with yours:
 try
        {
// your database operation, i.e add, remove
            ctx.SaveChanges();
        }
        catch (DbEntityValidationException e)
        {
            foreach (var eve in e.EntityValidationErrors)
            {
                Debug.WriteLine(@"Entity of type \"{0}\" in state \"{1}\" has the
                                 following validation errors:",
                    eve.Entry.Entity.GetType().Name, eve.Entry.State);
                foreach (var ve in eve.ValidationErrors)
                {
                    Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                        ve.PropertyName, ve.ErrorMessage);
                }
            }
        }





-KR



-KR


这篇关于MVC5存储库模式,“DbUpdateException未被用户代码处理”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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