I get the following error with the repository design pattern. I get this error from EfEntityRepositoryBase while inheriting EfCarDal in the Data Access layer. I couldn't overcome the problem.
Severity Code Description Project File Line Suppression State Error CS0311 The type 'Carebook.Dal.Concrete.EntityFramework.Conetext.AppDbContext' cannot be used as type parameter 'TContext' in the generic type or method 'EfEntityRepositoryBase<TEntity, TContext>'. There is no implicit reference conversion from 'Carebook.Dal.Concrete.EntityFramework.Conetext.AppDbContext' to 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext`
namespace Carebook.Core.DataAccess.Concrete
{
public class EfEntityRepositoryBase<TEntity, TContext> : IEntityRepository<TEntity>, IDisposable
where TEntity : class, IEntity, new()
where TContext : IdentityDbContext, new()
{
public void Add(TEntity entity)
{
using (TContext context = new TContext())
{
var addEntity = context.Entry(entity);
addEntity.State = EntityState.Added;
context.SaveChanges();
}
}
public void Delete(TEntity entity)
{
using (TContext context = new TContext())
{
var deleteEntity = context.Entry(entity);
deleteEntity.State = EntityState.Deleted;
context.SaveChanges();
}
}
public void Dispose()
{
throw new NotImplementedException();
}
public TEntity Get(Expression<Func<TEntity, bool>>? filter)
{
using (TContext context = new TContext())
{
return context.Set<TEntity>().SingleOrDefault(filter);
}
}
public List<TEntity> GetAll(Expression<Func<TEntity, bool>>? filter = null)
{
using (TContext context = new TContext())
{
return filter == null ?
context.Set<TEntity>().ToList() :
context.Set<TEntity>().Where(filter).ToList();
}
}
public void Update(TEntity entity)
{
using (TContext context = new TContext())
{
var updateEntity = context.Entry(entity);
updateEntity.State = EntityState.Modified;
context.SaveChanges();
}
}
}
}
namespace Carebook.Dal.Concrete.EntityFramework.Conetext
{
public class AppDbContext : IdentityDbContext<User, Role, int>
{
public AppDbContext(DbContextOptions options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
base.OnModelCreating(builder);
}
public virtual DbSet<Car> Cars { get; set; }
public virtual DbSet<CarPicture> CarPictures { get; set; }
public virtual DbSet<Feature> Features { get; set; }
public virtual DbSet<Reservation> Reservations { get; set; }
public virtual DbSet<Pricing> Pricings { get; set; }
public virtual DbSet<Contact> Contacts { get; set; }
}
}
namespace Carebook.Dal.Concrete.EntityFramework
{
public class EfCareDal: EfEntityRepositoryBase<Car,AppDbContext>,ICarDal
{
}
}
You are getting this error saying that
AppDbContextcannot be used as theTContexttype parameter in the generic classEfEntityRepositoryBase<TEntity, TContext>. The reason for this error is that the constraints onTContextexpect it to inherit fromIdentityDbContext, but as specified, it should inherit without any generic type parameters.In your code you have set the
AppDbContextinherit fromIdentityDbContext<User, Role, int>to resolve that you have to make sureEfEntityRepositoryBase<TEntity, TContext>can work withIdentityDbContext<TUser, TRole, TKey>instead of the non-genericIdentityDbContext.Below is the sample code you try: