Click here to Skip to main content
15,905,963 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.

C#
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; }

    }
}





This is my Homecontroller:

C#
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();


        }
}





And this is my businessLogic layer for the model: CustomerIdentityView
C#
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;
        }



    }
}
Posted
Updated 9-Jul-15 1:16am
v2

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
 
Share this answer
 
Comments
Janine_A 26-Jul-15 8:49am    
Thanks, it worked.
Just try to replace the following code with yours:
C#
 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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900