ArMaN
ArMaN

Reputation: 2407

no implicit reference conversion error in a generic repository and generic DbContext

I use a generic repository in my program but when I describe context as a generic context it causes a error:

Error 1 The type 'ArchiveBoundedContext.Infrastructure.Contexts.ArchiveBoundedContext' cannot be used as type parameter 'TContext' in the generic type or method 'SharedKernel.Infrastructure.Repository'. There is no implicit reference conversion from 'ArchiveBoundedContext.Infrastructure.Contexts.ArchiveBoundedContext' to 'System.Data.Entity.DbContext'. f:\Last Version Of Projects\Archiver-master\SharedKernel.Infrastructure\bin\Debug\SharedKernel.Infrastructure.dll ArchiveBoundedContext.Application

Generic Repository:

    public abstract class Repository<TEntity, TContext>
    where TEntity : class
    where TContext : DbContext, new()
{
    private DbSet<TEntity> _set;

    public TContext ActiveContext { get; set; }

    protected Repository()
    {
        ActiveContext = new TContext();
    }

    public DbSet<TEntity> Set
    {
        get { return _set ?? (_set = ActiveContext.Set<TEntity>()); }
    }

    public TEntity Create()
    {
        return Set.Create();
    }

    public int Add(TEntity item)
    {
        Set.Add(item);
        return ActiveContext.SaveChanges();
    }

    public int Remove(TEntity item)
    {
        Set.Remove(item);
        return ActiveContext.SaveChanges();
    }

    public int Update()
    {
        return ActiveContext.SaveChanges();
    }
}

ArchiveRepository:

public class ArchiveRepository : Repository<PersonIdentificationImage, Contexts.ArchiveBoundedContext>, IArchiveRepository
{
    public ArchiveRepository() { }

    public void AddPersonIdentificationImage(PersonIdentificationImage personIdentificationImage)
    {
        var ctx = ActiveContext;
        ctx.PersonIdentificationImages.Add(personIdentificationImage);
    }
}

ArchiveService:

public class ArchiveService : IArchiveService
{
    private readonly ArchiveRepository _archiveRepository;

    public ArchiveService()
    {
        _archiveRepository = new ArchiveRepository();
    }
}

QTasArchiveBaseContext:

public class QTasArchiveBaseContext<TContext> : DbContext where TContext : DbContext 
{
    static QTasArchiveBaseContext()
    {
        Database.SetInitializer<TContext>(null);
    }

    protected QTasArchiveBaseContext()
        : base(DatabaseSettings.Default.QTasArchiveDatabaseConString)
    { }
}

ArchiveBoundedContext:

public class ArchiveBoundedContext : QTasArchiveBaseContext<ArchiveBoundedContext>
{
    public DbSet<PersonOtherImage> PersonScannedImages { get; set; }

    public DbSet<PersonIdentificationImage> PersonIdentificationImages { get; set; }
}

Upvotes: 1

Views: 2477

Answers (1)

Fabio
Fabio

Reputation: 11990

In the Repository class, replace the DbContext for class:

  public abstract class Repository<TEntity, TContext>
    where TEntity : class
    where TContext : DbContext, new()

  public abstract class Repository<TEntity, TContext>
        where TEntity : class
        where TContext : class
{
     private DbSet<TEntity> _set;

     public TContext ActiveContext { get; set; }

     public Repository(TContext dbContext)
     {
          ActiveContext = dbContext;
     }

     ....
}

Does that work?

Upvotes: 1

Related Questions