How to get data from additional tables using EF/LINQ

Atul Sureka

I have following three tables

Employee(empid, name)
Role(roleid, name)
EmpRoleMap(mapid, empid,roleid)

In the index view I show the link of employees with edit link with each employee. When user clicks on edit it opens the edit screeen for the selected employee.

For this page to render I am making two calls to database. First is to get the details about selected employee. Seconds is to query the EmpRoleMap table to find out what all roles that employee belongs to.

I want to do the same stuff in single database call, i.e single call would give me the details about the employee and the roleID from EmpRoleMap & RoleName list for the given Employee.

I am using Code First approach with Scaffolding.

How can I do this using EntityFramework? Can we use "Include" clause with LINQ to get the additional data from other tables (here EmpRoleMap,Role).

Antonio Petricca

You have to do a good Entity Model design. I'm going to try to show you an example of what I'm saying, but remember that at this moment I cannot check the code.

    public class EmployeeModel
    {
        [Key]
        [ForeignKey("EmployeeRoles")]

        public decimal empid { get; set; }

        public string name { get; set; }

        public virtual List<EmpRoleModel> EmployeeRoles { get; set; }
    }

    public class RoleModel
    {
        [Key]
        public decimal roleid { get; set; }

        public string name { get; set; }
    }

    public class EmpRoleModel
    {
        [Key]
        [Column(Order = 0)]
        [ForeignKey("Employee")]

        public decimal empid { get; set; }

        [Key]
        [Column(Order = 1)]
        [ForeignKey("Role")]

        public decimal roleid { get; set; }

        public virtual EmployeeModel Employee { get; set; }
        public virtual RoleModel     Role     { get; set; }
    }

Now you can navigate to employee roles by the EmployeeRoles property.

Please try this code and give me feedback.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get additional and missed data from 2 excels using Python

How to get additional column with row count using two different tables?

How to get rows of Data from Multiple Tables using LinQ To Entities

How to get data from other tables using Sequel

How can I get data from joined tables using fetchxml

how to get data from multiple tables in sql using php

how to get data from two tables Using JOIN

How to get data from multiple tables?

LINQ - How to get data from multiple tables

How get data in method from others tables?

How to get data from n:m tables

How to get tables of data from JupyterLab to Excel

How can I get additional data from one promise to another?

How to get data from additional table in many to many relationship?

How to pass and get data from Ajax to PHP using Data-tables

How to join 4 tables to get data from two tables

Get data from tables

How to JOIN two SQL tables to get a specific additional data based on matching id's

Get multiple data from tables Using Entity Framework data model

How do i get data from two tables using single query in mysql

How to get all column data from two tables by using leftJoin in Yii2

How to get data from multiple tables using just one single query?

How to get data from two tables from database with MySql

Cant get data from 2 tables using containable in cakephp

Cannot get data from two tables using join

Django: get data from tables and display together using ListView

Get multiple data from tables Using Entity Framework

mysql get auction data and bids from 2 tables using JOIN

Using a SELECT query to get data from two different tables in database