【问题标题】:Value from generic class to generic base class从泛型类到泛型基类的值
【发布时间】:2018-01-13 09:54:42
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    问题

    因为你在这里传递了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()) { }
    }
    

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

    【讨论】:

    • 你没有阅读。我期待这个答案,但我不认为是这样。如果是这样,那么如何通过 DbContext 呢?
    • @Kris-I 我已经更新了我的答案,向你展示为什么它是正确的。
    • 好的,明白了。但是,那么如何创建 DBContext 呢? (带新)
    • @Kris-I - 我为您更新了几个选项的答案。你还应该看看你是如何添加服务的,你不需要使用typeof(xxx)docs.microsoft.com/en-us/aspnet/core/fundamentals/…
    • @Kris-I 您的问题在哪里? services.AddTransient() ?
    猜你喜欢
    • 2014-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2019-08-12
    • 2019-05-30
    • 2012-09-08
    相关资源
    最近更新 更多