【问题标题】:no implicit reference conversion error in a generic repository and generic DbContext通用存储库和通用 DbContext 中没有隐式引用转换错误
【发布时间】:2015-08-18 12:37:00
【问题描述】:

我在我的程序中使用了通用存储库,但是当我将上下文描述为通用上下文时,它会导致错误:

错误 1 ​​类型 'ArchiveBoundedContext.Infrastructure.Contexts.ArchiveBoundedContext' 不能用作类型参数 'TContext' 在泛型类型或 方法 'SharedKernel.Infrastructure.Repository'。 没有来自的隐式引用转换 'ArchiveBoundedContext.Infrastructure.Contexts.ArchiveBoundedContext' 到“System.Data.Entity.DbContext”。 f:\Last Version Of Projects\Archiver-master\SharedKernel.Infrastructure\bin\Debug\SharedKernel.Infrastructure.dll ArchiveBoundedContext.Application

通用存储库:

    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);
    }
}

存档服务:

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; }
}

【问题讨论】:

  • ArchiveBoundedContext.Infrastructure.Contexts.ArchiveBoundedContext 的定义在哪里?
  • @David 将 ArchiveBoundedContext 添加到我的问题末尾。
  • 还有...QTasArchiveBaseContext&lt;T&gt; 是什么?根据错误ArchiveBoundedContext 不能用作DbContext。应该吗?
  • @David 我在我的问题中描述了 ArchiveBoundedContext。它在 ArchiveBoundedContext 之前。公共类 QTasArchiveBaseContext : DbContext where TContext : DbContext { }
  • 为什么你的存储库继承自 IEnumerable?

标签: c# generics inheritance interface


【解决方案1】:

在Repository 类中,将DbContext 替换为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;
     }

     ....
}

这行得通吗?

【讨论】:

  • 不,如果我将 TContext 描述为类并且我不能使用像 SaveChanges() 这样的函数。
  • 错误 1 ​​'TContext' 是一个'类型参数',但用作'变量' F:\Last Version Of Projects\Archiver-master\SharedKernel.Infrastructure\Repository.cs 21 40 SharedKernel。基础设施
  • 对不起,我的错误。您必须在构造函数中传递 dbContext。构造函数必须是公共的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-28
相关资源
最近更新 更多