Click here to Skip to main content
15,880,405 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
My MVC Application

Where my Controller action method is

C#
public class EmployeeController : Controller
    {

        public ActionResult Index(int Id)
        {
            Employee_Context Emp = new Employee_Context();

            Employee_Details Emp_Detail = Emp.Employee.Single(emp => emp.id == Id);
            return View(Emp_Detail);
    }
}


My Model

C#
public class Employee_Context : DbContext
   {
       public DbSet<Employee_Details> Employee { get; set; }

   }


C#
namespace MvC_Demo1.Models
{
     [Table("Ëmployee")]
    public class Employee_Details
    {
        public int id { get; set; }
        public string Name { get; set; }
        public string Designation { get; set; }
    }
}


My ViewModel

HTML
@model MvC_Demo1.Models.Employee_Details


@{
    ViewBag.Title = "Index";
}

<div style="font-style:italic">
    <h2>Employee Details</h2>
    <table>
        <tr>
            <td>Id</td>
            <td>@Model.id</td>
        </tr>
        <tr>
            <td>Name</td>
            <td>@Model.Name</td>

        </tr>
        <tr>
            <td>Designation</td>
            <td>@Model.Designation</td>

        </tr>

    </table>
</div>



am getting this Execption in Controller action method which I mentioned it below

Employee_Details Emp_Detail = Emp.Employee.Single(emp => emp.id == Id);


Please give a solution for this am new to MVC
Posted
Updated 16-Apr-17 7:42am
v2
Comments
Member 11542299 13-Jan-16 14:43pm    
I'm also facing the Same satuation Like you . if your problem will be solved the help me.

Hi,

Check if you have typed correctly the target table name, I am not sure if the issue is related to it but I see that you may have mistyped a special char (Ë) while specifying the target table name.

Try changing Data Annotation [Table("Ëmployee")] for [Table("Employee")]

Regards,



Jose N. Estrella
 
Share this answer
 
Check Your Connection string in web config i am also getting the same error it is due to the Connection string was not as want
 
Share this answer
 
Hello,

Some of your database entity is mismatching. Please check the db entities being used in this method.

For ref: c# - System.Data.Entity.Core.EntityCommandExecutionException Occurred in MVC app Using EF[^]

Thanks
 
Share this answer
 
C#
Hello,

namespace MvC_Demo1.Models
{
     [Table("Ëmployee")]
    public class Employee_Details
    {
        public int id { get; set; }
        public string Name { get; set; }
        public string Designation { get; set; }
    }
}

Check the fields id, Name, Designation must be same as column name in your table.

If it mismatched, then you will get error like this..!

So change the correct field names according to table column names
 
Share this answer
 
Hello,
i get the same error its may be because in your db you may have define Employeeid a primary key and here in your
Employee_Details
class you have not define a key so just add [Key] before id attribute :)
 
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