泛型类到泛型基类

时间:2017-08-05 16:27:07

标签: c# .net entity-framework inheritance asp.net-web-api2

在WebApi Core中,我通过AddDataAccess将上下文传递给服务集合。我真的不明白为什么上下文在这一行是空的:

var res = this.Context.Set<TEntity>().AsEnumerable();

有些人会说,因为构造函数中的参数中有null,但对我来说是正确的。应该是别的东西。

//在Stratup.cs中

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<myConnectionStringContext>
        (options => options.UseSqlServer(_config["ConnectionStrings:myConnectionString"]));
    services.AddDataAccess<myConnectionStringContext>();

    // Add framework services.
    services.AddMvc();
}

//在我的个人资料包中

    public static class ServiceCollectionExtentions
    {
        public static IServiceCollection AddDataAccess<TEntityContext>(this IServiceCollection services) 
            where TEntityContext : DbContext
        {
            RegisterDataAccess<TEntityContext>(services);
            return services;
        }

        private static void RegisterDataAccess<TEntityContext>(IServiceCollection services) 
            where TEntityContext : DbContext
        {
            services.AddTransient(typeof(TEntityContext));
            services.AddTransient(typeof(IRepository<>), typeof(GenericEntityRepository<>));
        }
    }

public class EntityBase
{
    [Key]
    public int Id { get; set; }
}

public interface IRepository<TEntity>
{
    IEnumerable<TEntity> GetAll();
}

public class GenericEntityRepository<TEntity> : EntityRepositoryBase<DbContext, TEntity> 
    where TEntity : EntityBase, new()
{
    public GenericEntityRepository() : base(null)
    { }
}

public abstract class EntityRepositoryBase<TContext, TEntity> : RepositoryBase<TContext>, IRepository<TEntity> 
    where TContext : DbContext where TEntity : EntityBase, new()
{
    protected EntityRepositoryBase(TContext context) : base(context)
    { }

    public virtual IEnumerable<TEntity> GetAll()
    {
        return this.Context.Set<TEntity>().AsEnumerable();
    }
}

public abstract class RepositoryBase<TContext> where TContext : DbContext
{
    protected TContext Context { get; private set; }

    protected RepositoryBase( TContext context)
    {
       this.Context = context;
    }
}

1 个答案:

答案 0 :(得分:0)

问题

  

因为你在这里传递null

public GenericEntityRepository() : base(null) // <-- passing null

这将转到父EntityRepositoryBase

protected EntityRepositoryBase(TContext context) : base(context) // <--(1) context = null

然后转到父RepositoryBase

protected TContext Context { get; private set; } // <--(4) context = null

protected RepositoryBase(TContext context) // <--(2) context = null
{
    this.Context = context; // <--(3) context = null
}

所以当你打电话

    return this.Context.Set<TEntity>().AsEnumerable();
this.Context // <-- this is null

解决方案

您需要将GenericEntityRepository从发送null更改为发送dbContext

public class GenericEntityRepository<TEntity> : EntityRepositoryBase<DbContext, TEntity>
    where TEntity : EntityBase
{
    public GenericEntityRepository(ApplicationDbContext dbContext) : base(dbContext) { }
}

或者,你也可以(优先)

public class GenericEntityRepository<TContext, TEntity> : EntityRepositoryBase<TContext, TEntity>
    where TContext: DbContext, new()
    where TEntity : EntityBase
{
    public GenericEntityRepository() : base(new TContext()) { }
}
  

注意:使用您提供的代码,您可能会取消其中的几个通用类,因为除了不必要的继承之外,它们不提供任何功能。