You must Sign In to post a response.
  • Category: ASP.Net MVC

    How the data is retrieved from database using linq in entity framework

    The following question is asked in an interview and i don't know the answer.

    How the data is retrieved from database using linq in entity framework

    Any help is appreciated
  • #767741
    Hai Rajan,
    Entity Framework supports Table, View and Stored Procedure to convert in to classes, members and Methods/Functions.
    So once you get the converted C# objects from the database objects, you can use the DataContext class to get the data.
    For e.g. There is a table with the name 'Employee' in the database. This table contains EmpId, FirstName and LastName, Salary fields/columns.
    Once you convert this table using Entity Framework, the C# class with the name Employee and the public properties with the name EmpId, FirstName, LastName and Salary will get created automatically.

    public class Employee
    {
    public int EmpId {get; set;}
    public string FirstName {get; set;}
    public string LastName {get; set;}
    public double Salary {get; set;}
    }

    Now to retrieve the data from this Employee table, you just need DataContext class:

    DataContext dc= new DataContext();
    List<Employee> employeeList = dc.Employees.ToList();

    This will give the list of employees from the database.
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


  • Sign In to post your comments