Use Generic repository with different id name
JJ TT
141
Reputation points
Goal:
Use Generic repository when you have table's id with different name. The different name is for instance 'TTest1Id', 'TTest2id' etc. It is not name as 'Id'.
Problem:
When I use Generic repository in relation to table's id name id, it works.
How should I use generic repository in relation different id name (in each table) for instance 'TTest1Id', 'TTest2id' etc?
Thank you!
namespace WebApplication1.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private SakuraContext _db;
private readonly IGenericRepositoryCommand<Ttest1> _repositoryTtest1 = null;
private readonly IGenericRepositoryCommand<Ttest2> _repositoryTtest2 = null;
public WeatherForecastController(
JanaruContext db,
IGenericRepositoryCommand<Ttest1> repositoryTtest1,
IGenericRepositoryCommand<Ttest2> repositoryTtest2)
{
this._db = db;
this._repositoryTtest1 = repositoryTtest1;
this._repositoryTtest2 = repositoryTtest2;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
var test = _repositoryTtest1.GetById(1);
return null;
}
}
}
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1.Data
{
public interface IGenericRepositoryCommand<T> where T : class
{
public IQueryable<T> GetAll();
Task<ICollection<T>> GetAllAsync();
IQueryable<T> FindBy(Expression<Func<T, bool>> predicate);
Task<ICollection<T>> FindByAsync(Expression<Func<T, bool>> predicate);
T GetById(int id);
Task<T> GetAsync(int id);
Task AddAsync(T obj, bool detachAllAfterSave = false);
Task UpdateAsync(T obj);
Task DeleteAsync(T entity);
T Add(T t, bool detachAllAfterSave = false);
T Update(T t, object key);
void Delete(T entity);
void AddRange(IEnumerable<T> entities);
void RemoveRange(IEnumerable<T> entities);
Task AddRangeAsync(IEnumerable<T> entities);
}
public class GenericRepositoryCommand<T> : IGenericRepositoryCommand<T> where T : class
{
private readonly SakuraContext _context = null;
/*
public GenericRepositoryCommand()
{
this._context = new TableContext();
table = _context.Set<T>();
}
*/
public GenericRepositoryCommand(SakuraContext context)
{
this._context = context;
}
public IQueryable<T> GetAll()
{
return this._context.Set<T>();
}
public async Task<ICollection<T>> GetAllAsync()
{
return await _context.Set<T>().ToListAsync();
}
public virtual IQueryable<T> FindBy(Expression<Func<T, bool>> predicate)
{
IQueryable<T> query = _context.Set<T>().Where(predicate);
return query;
}
public virtual async Task<ICollection<T>> FindByAsync(Expression<Func<T, bool>> predicate)
{
return await _context.Set<T>().Where(predicate).ToListAsync();
}
public T GetById(int id)
{
return this._context.Set<T>().Find(id);
}
public async Task<T> GetAsync(int id)
{
CREATE TABLE [dbo].[TTest1](
[id] [int] IDENTITY(1,1) NOT NULL,
[isbn] [varchar](50) NOT NULL,
[date] [datetime] NOT NULL,
[score] [int] NOT NULL,
PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[TTest2](
[id] [int] IDENTITY(1,1) NOT NULL,
[isbn] [varchar](50) NOT NULL,
[date] [datetime] NOT NULL,
[score] [int] NOT NULL,
PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
Developer technologies .NET Other
4,103 questions
Developer technologies C#
11,570 questions
Sign in to answer