Reputation: 11
I am new to mvc. And I am getting System.Data.Entity.Core.EntityException in mvc.
My Model file contains below code:
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace demomvc.Models
{
[Table("Employee")]
public class Employee
{
[Key]
public int EmployeeId {get;set;}
public string EmployeeName { get; set; }
}
public class DbClass : DbContext
{
public DbSet<Employee> employee { get; set; }
}
}
My View Contains following Code:
@model IEnumerable<demomvc.Models.Employee>
@using demomvc.Models;
@{
ViewBag.Title = "Index";
}
<h2>Employee</h2>
@foreach(Employee emp in @Model)
{
<li>
@emp.EmployeeId;
@emp.EmployeeName;
</li>
}
Controller is:
Web.Config Code:
<add name="DbClass" connectionString="Data Source=DIPS;
Initial Catalog=dbo.Employee;Integrated Security=True"
providerName="System.Data.SqlClient" />
SQL server Image:
Please help me to resolve a problem.
Upvotes: 1
Views: 276
Reputation: 2469
The connection string must point to your database, not to your table:
<add name="DbClass" connectionString="Data Source=DIPS;Initial Catalog=dips;User ID=sa;Password=dips" providerName="System.Data.SqlClient" />
Also mark the property as virtual::
public virtual DbSet<Employee> employee { get; set; }
Upvotes: 1