sql - How to get foreign key by sending primary key in mVC? -
i working on mini project in mvc in performing crud operations. have 3 tables: employee: pk: empid salary: fk: empid images: fk: empid navigating between 2 html pages , passing pk one.html two.html. want access data of salary , images table on two.html, have primary key of employee table , foreign key in rest 2 tables.
i want read data attached empid treated fk in salary , images table. empid has 1 row in employee table (pk) empid has multiple rows in salary , images table (fk)
thank in advance
var employeeimages = _dbcontext.images.where(i => i.empid == 21).tolist();
where 21
employee id parameter received in action method.
similarly can query salaries like:
var employeesalaries = _dbcontext.salaries.where(i => i.empid == 21).tolist();
or both things in same call:
var employee = _dbcontext.employee.where(e => e.id == 21).include(e => e.images).include(e => e.salaries).tolist();
if working on production application, calling dbcontext
directly controller not idea. might want @ ef's documentation.
also, when working @ controller layer, don't think in terms of fks , pks. domain models. think these in terms of identifiers. fk , pks come game once reach database level (which shouldn't controller @ least).
Comments
Post a Comment