Search This Blog

Monday 22 February 2016

"An error occurred while executing the command definition. See the inner exception for details."

When you are getting following error, check your model class mapping properties filed matching with your SQL server table column or not.

If any of the properties don't match with SQL Server table columns you will get the following error.


In my case, I am using the Address properties field in Employee model class mapping, i.e. not exist in the SQL Server Employee table.




Employee Model Class Mapping

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace MvcDemo.Models
{
    [Table("tblEmployee")]
    public class Employee
    {
        [Key]
        public int EmployeeId { getset; }
        public string Name { getset; }
        public string Gender { getset; }
        public string City { getset; }
        public string Address { getset; }
    }
}

Employee Table Structure in SQL Server

Create table tblEmployee
(
       EmployeeId int Primary Key Identity(1,1),
       Name nvarchar(50),
       Gender nvarchar(10),
       City nvarchar(50),
       DateOfBirth DateTime,
       DepartmentId int

)

No comments:

Post a Comment