2017-08-05 73 views
-1

在WebApi Core中,我通過AddDataAccess將上下文傳遞給服務集合。我真的不明白爲什麼上下文是在該行空:從泛型類到泛型基類的值

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

有人會說,因爲在構造函數中的參數,但我它的正確有空。應該是別的東西。

//在Stratup.cs

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

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

//在我的personnal包

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

回答

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()) { } 
} 

注意:使用您提供的代碼,您可能會取消這些泛型類,因爲除了不必要地繼承外,它們不起任何作用。

+0

您沒有閱讀。我期待這個答案,但我不認爲是這樣。如果這是如何傳遞DbContext呢? –

+0

@ Kris-I我已經更新了我的答案,向你展示了爲什麼這是真的。 – Svek

+0

好了解。但是,如何創建DBContext呢? (帶新) –