1

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:

Error in Controller

Web.Config Code:

     <add name="DbClass" connectionString="Data Source=DIPS;
     Initial Catalog=dbo.Employee;Integrated Security=True"
      providerName="System.Data.SqlClient" /> 

SQL server Image:

SQL server Image

Please help me to resolve a problem.

8
  • Try using username and password for the connection string <add name="DbClass" connectionString="Data Source=DIPS; Initial Catalog=dbo.Employee;User ID=usernamehere;Password=passwordhere" providerName="System.Data.SqlClient" /> Commented Mar 19, 2017 at 11:53
  • i have provided UserID=sa;Password=dips .. But it is giving the same error Commented Mar 19, 2017 at 12:01
  • Make this virtual public virtual DbSet<Employee> employee { get; set; } Commented Mar 19, 2017 at 12:07
  • 'System.ArgumentException' occurred in System.Data.dll but was not handled in user code. Keyword UserID is not supported. Commented Mar 19, 2017 at 12:15
  • Is User ID= not UserID. Commented Mar 19, 2017 at 12:16

1 Answer 1

1

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; }
Sign up to request clarification or add additional context in comments.

2 Comments

Initial catlog value was wrong in my connection string. I don't understand why Dbset class is virtual.?
The virtual keyword is used to enable lazy loading mechanism as long as the dbcontext is alive. This means the employees will get loaded only when is required (or accessed). It's also common to use virtual for the testing purposes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.