Click here to Skip to main content
16,017,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Problem

How to implement repository pattern in asp.net core 2.1 visual studio 2017 with SQL server 2012 ?

i work on project work on repository pattern but i cannot implement it




1- How to define repository on controller Employee to call function GetAll on constructor?

meaning how to define repository on constructor of controller

2- what i write on startup to define these modules and connection ?

3- suppose i have compost key how to use getbyid suppose i have (Employeid,branchcode) as compost key ?

are compost key define as fluent api or annotation or what ?

please can you help me and give me details if possible

can any one answer me of this question please ?

What I have tried:

model Employee

public class Employee   
{   
public int EmployeeId { get; set; }   
public int BranchCode { get; set; }   
public string EmployeeName { get; set; }   
public int EmployeeAge { get; set; }   
}   
Repository public class RepositoryTab<T> : IrepositoryTab<T> where T : class   
{   
protected TabDbContext db { get; set; }   
private DbSet<T> dbSet;  
 public RepositoryTab(TabDbContext Tabdb)   
{              
 db = Tabdb;              
 dbSet = db.Set<T>();   
}   
public IEnumerable<T> GetAll()   
{  
 return dbSet.ToList();  
 }   
}   
public interface IrepositoryTab<T> where T : class { IEnumerable<T> GetAll();        
T GetById(object Id);  
 }  
Posted
Updated 16-Jan-19 22:46pm

1 solution

1) You're thinking of "composite keys":

c# - Mapping composite keys using EF code first - Stack Overflow[^]

2) The "generics" (almost) never justify themselves.

Create "meaningful" statics like "GetCustomerByXxxx". (or something)

"GetAll()" implies no consideration for performance and should automatically flag in an employee's file.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900