{"rsdb":{"rid":"377550","subhead":"","postdate":"0","aid":"264191","fid":"106","uid":"1","topic":"1","content":"
\n

\u4e00\u3001\u95ee\u9898<\/h2> \n

\u8be5\u95ee\u9898\u7ecf\u5e38\u51fa\u73b0\u5728 ABP vNext \u6846\u67b6\u5f53\u4e2d\uff0c\u8981\u590d\u73b0\u8be5\u95ee\u9898\u5341\u5206\u7b80\u5355\uff0c\u53ea\u9700\u8981\u4f60\u6ce8\u5165\u4e00\u4e2a IRepository<T,TKey><\/code> \u4ed3\u50a8\uff0c\u5728\u4efb\u610f\u4e00\u4e2a\u5730\u65b9\u8c03\u7528 IRepository<T,TKey>.ToList()<\/code> \u65b9\u6cd5\u3002<\/p> \n

[Fact]\npublic void TestMethod()\n{\n    var rep = GetRequiredService<IHospitalRepository>();\n\n    var result = rep.ToList();\n}<\/code><\/pre> \n 

\u4f8b\u5982\u4e0a\u9762\u7684\u6d4b\u8bd5\u4ee3\u7801\uff0c\u4e0d\u51fa\u610f\u5916\u5c31\u4f1a\u63d0\u793a System.ObjectDisposedException<\/code> \u5f02\u5e38\uff0c\u5177\u4f53\u7684\u5f02\u5e38\u5185\u5bb9\u4fe1\u606f\uff1a<\/p> \n

System.ObjectDisposedException : Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.<\/code><\/pre> \n 

\u5176\u5b9e\u5df2\u7ecf\u8bf4\u5f97\u5341\u5206\u660e\u767d\u4e86\uff0c\u56e0\u4e3a\u4f60\u8981\u8c03\u7528\u7684 DbContext<\/code> \u5df2\u7ecf\u88ab\u91ca\u653e\u4e86\uff0c\u6240\u4ee5\u4f1a\u51fa\u73b0\u8fd9\u4e2a\u5f02\u5e38\u4fe1\u606f\u3002<\/p> \n

\u4e8c\u3001\u539f\u56e0<\/h2> \n

2.1 \u4e3a\u4ec0\u4e48\u80fd\u591f\u8c03\u7528 LINQ \u6269\u5c55?<\/h3> \n

\u6211\u4eec\u4e4b\u6240\u4ee5\u80fd\u591f\u5728 IRepository<TEntity,TKey><\/code> \u63a5\u53e3\u4e0a\u9762\uff0c\u8c03\u7528 LINQ \u76f8\u5173\u7684\u6d41\u7545\u63a5\u53e3\uff0c\u662f\u56e0\u4e3a\u5176\u7236\u7ea7\u63a5\u53e3 IReadOnlyRepository<TEntity,TKey><\/code> \u7ee7\u627f\u4e86 IQueryable<TEntity><\/code> \u63a5\u53e3\u3002\u5982\u679c\u4f7f\u7528\u7684\u662f Entity Framework Core \u6846\u67b6\uff0c\u90a3\u4e48\u5728\u89e3\u6790 IRepository<T,Key><\/code> \u7684\u65f6\u5019\uff0c\u6211\u4eec\u5f97\u5230\u7684\u662f\u4e00\u4e2a EfCoreRepository<TDbContext, TEntity,TKey><\/code> \u5b9e\u4f8b\u3002<\/p> \n

\u9488\u5bf9\u8fd9\u4e2a\u5b9e\u4f8b\uff0c\u7c7b\u578b EfCoreRepository<TDbContext, TEntity><\/code> \u5219\u662f\u5b83\u7684\u57fa\u7c7b\u578b\uff0c\u7ee7\u7eed\u8df3\u8f6c\u5230\u5176\u57fa\u7c7b RepositoryBase<TEntity><\/code> \u6211\u4eec\u5c31\u80fd\u770b\u5230\u5b83\u5b9e\u73b0\u4e86 IQueryable<T><\/code> \u63a5\u53e3\u5fc5\u5907\u7684\u51e0\u4e2a\u5c5e\u6027\u3002<\/p> \n

public abstract class RepositoryBase<TEntity> : BasicRepositoryBase<TEntity>, IRepository<TEntity>\n    where TEntity : class, IEntity\n{\n    \/\/ ... \u5ffd\u7565\u7684\u4ee3\u7801\u3002\n    public virtual Type ElementType => GetQueryable().ElementType;\n\n    public virtual Expression Expression => GetQueryable().Expression;\n\n    public virtual IQueryProvider Provider => GetQueryable().Provider;\n\n    \/\/ ... \u5ffd\u7565\u7684\u4ee3\u7801\u3002\n\n    IEnumerator IEnumerable.GetEnumerator()\n    {\n        return GetEnumerator();\n    }\n\n    public IEnumerator<TEntity> GetEnumerator()\n    {\n        return GetQueryable().GetEnumerator();\n    }\n\n    protected abstract IQueryable<TEntity> GetQueryable();\n\n    \/\/ ... \u5ffd\u7565\u7684\u4ee3\u7801\u3002\n}<\/code><\/pre> \n 

2.2 IQueryable \u4f7f\u7528\u7684 DbContext<\/h3> \n

\u4e0a\u4e00\u4e2a\u5c0f\u8282\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u51fa\u6700\u540e\u7684 IQueryable<TEntity><\/code> \u662f\u901a\u8fc7\u62bd\u8c61\u65b9\u6cd5 GetQueryable()<\/code> \u53d6\u5f97\u7684\u3002\u8fd9\u4e2a\u62bd\u8c61\u65b9\u6cd5\uff0c\u5728 EF Core \u5f53\u4e2d\u7684\u5b9e\u73b0\u5982\u4e0b\u3002<\/p> \n

public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IEfCoreRepository<TEntity>\n    where TDbContext : IEfCoreDbContext\n    where TEntity : class, IEntity\n{\n    public virtual DbSet<TEntity> DbSet => DbContext.Set<TEntity>();\n\n    DbContext IEfCoreRepository<TEntity>.DbContext => DbContext.As<DbContext>();\n\n    protected virtual TDbContext DbContext => _dbContextProvider.GetDbContext();\n\n    private readonly IDbContextProvider<TDbContext> _dbContextProvider;\n\n    \/\/ ... \u5ffd\u7565\u7684\u4ee3\u7801\u3002\n\n    public EfCoreRepository(IDbContextProvider<TDbContext> dbContextProvider)\n    {\n        _dbContextProvider = dbContextProvider;\n\n        \/\/ ... \u5ffd\u7565\u7684\u4ee3\u7801\u3002\n    }\n\n    \/\/ ... \u5ffd\u7565\u7684\u4ee3\u7801\u3002\n\n    protected override IQueryable<TEntity> GetQueryable()\n    {\n        return DbSet.AsQueryable();\n    }\n\n    \/\/ ... \u5ffd\u7565\u7684\u4ee3\u7801\u3002\n}<\/code><\/pre> \n 

\u6240\u4ee5\u6211\u4eec\u5c31\u53ef\u4ee5\u77e5\u9053\uff0c\u5f53\u8c03\u7528 IQueryable<TEntity>.ToList()<\/code> \u65b9\u6cd5\u65f6\uff0c\u5b9e\u9645\u662f\u4f7f\u7528\u7684 IDbContextProvider<TDbContext><\/code> \u89e3\u6790\u51fa\u6765\u7684\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5bf9\u8c61\u3002<\/p> \n

\u8df3\u8f6c\u5230\u8fd9\u4e2a DbContextProvider \u7684\u5177\u4f53\u5b9e\u73b0\uff0c\u53ef\u4ee5\u770b\u5230\u4ed6\u662f\u901a\u8fc7 IUnitOfWorkManager<\/code>(\u5de5\u4f5c\u5355\u5143\u7ba1\u7406\u5668<\/strong>) \u5f97\u5230\u53ef\u7528\u7684\u5de5\u4f5c\u5355\u5143\uff0c\u7136\u540e\u901a\u8fc7\u5de5\u4f5c\u5355\u5143\u63d0\u4f9b\u7684 IServiceProvider<\/code> \u89e3\u6790\u6240\u9700\u8981\u7684\u6570\u636e\u5e93\u4e0a\u4e0b\u6587\u5bf9\u8c61\u3002<\/p> \n

public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbContext>\n    where TDbContext : IEfCoreDbContext\n{\n    private read","orderid":"0","title":"ABP vNext \u4e0d\u4f7f\u7528\u5de5\u4f5c\u5355\u5143\u4e3a\u4ec0\u4e48\u4f1a\u629b\u51fa\u5f02\u5e38(\u4e00)","smalltitle":"","mid":"0","fname":".NET","special_id":"0","bak_id":"0","info":"0","hits":"515","pages":"3","comments":"0","posttime":"2019-10-10 18:14:57","list":"1570702497","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"ABP<\/A> vNext<\/A> \u4f7f\u7528<\/A> \u5de5\u4f5c<\/A> \u5355\u5143<\/A> \u4e3a\u4ec0\u4e48<\/A> \u5f02\u5e38<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"14.17.22.37","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"ABP vNext \u4e0d\u4f7f\u7528\u5de5\u4f5c\u5355\u5143\u4e3a\u4ec0\u4e48\u4f1a\u629b\u51fa\u5f02\u5e38","lastview":"1716082053","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}