repository design pattern identitydbcontext problem

52 views Asked by At

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
    {


    }
}
1

There are 1 answers

0
Jalpa Panchal On

You are getting this error saying that AppDbContext cannot be used as the TContext type parameter in the generic class EfEntityRepositoryBase<TEntity, TContext>. The reason for this error is that the constraints on TContext expect it to inherit from IdentityDbContext, but as specified, it should inherit without any generic type parameters.

In your code you have set the AppDbContext inherit from IdentityDbContext<User, Role, int> to resolve that you have to make sure EfEntityRepositoryBase<TEntity, TContext> can work with IdentityDbContext<TUser, TRole, TKey> instead of the non-generic IdentityDbContext.

Below is the sample code you try:

public class EfEntityRepositoryBase<TEntity, TContext> : IEntityRepository<TEntity>, IDisposable
    where TEntity : class, IEntity, new()
    where TContext : IdentityDbContext<User, Role, int>, new() 
{
    // Your original code
}