I have 3 classes, Client
, Payee
and LookupFile
, and I'm planning to use a generic repository pattern to work with the database.
The LookupFile
and Payee
always have a Client
associated with them.
When querying for a LookupFile
or Payee
, I want to include the related Client
so I included the Client
object in the base entity.
Now this is creating a self referencing key in the Client
table when I run migrations.
How do I go about not having a self referencing Client
table and also be able to include the related Client
record when querying the LookupFile
and Payee
?
public class BaseEntity
{
public int Id { get; set; }
public Client Client { get; set; } //Added client object so it be used in include
}
public class Payee : BaseEntity
{
public int ClientId { get; set; }
public string PayeeName { get; set; }
}
public class LookupFile : BaseEntity
{
public int ClientId { get; set; }
public string FileName { get; set; }
}
public class Client : BaseEntity
{
public string ClientName { get; set; }
}
// Then my GenericRepository is defined as below:
public class DataRepository<T> : IBaseRepository<T> where T : BaseEntity
{
private readonly ApplicationDbContext _context;
public DataRepository(ApplicationDbContext context)
{
_context = context;
}
public async Task<IEnumerable<T>> GetAll()
{
return await _context.Set<T>()
.AsNoTracking()
.Include(c => c.Client)
.ToListAsync();
}
}