EF Core temporary table operations

Internet to find the two, a memory table, a temporary table.

1 installation package: EntityFrameworkCore.MemoryJoin // memory table directly select union stitching inquiry  http://tsherlock.tech/2018/03/20/joining-in-memory Entity-to---list-Query Framework /
the DbContext added class:  protected DbSet <QueryModelClass> {QueryData is GET; SET;} // remove upsized note the code table upsized code

2 references:  https://dev.azure .com / pawelgerr / _git / Thinktecture.EntityFrameworkCore  ; or:  https://github.com/yangzhinong/Thinktecture.EntityFrameworkCore  DLL compiled
ConfigureServices method added: 
          services.AddDbContext<SalesDbContext>(options => {
                options.UseSqlServer(_appConfiguration.GetConnectionString("Default"), opts => {
                    opts.AddTempTableSupport();
                });
            });
3. Define the interface and implementation of common Repository
3.1 public interface ITempTableRepository {
        IQueryable<T> FromLocalList<T>(IList<T> data) where T : class;
        Task<IQueryable<T>> BulkInsertIntoTempTableAsync<T>(IEnumerable<T> entities) where T : class;
 }
3.2 public class TempTableRepository : SalesRepositoryBase<CodeTemplate<Guid>, Guid>, ITempTableRepository {
        public TempTableRepository(IDbContextProvider<SalesDbContext> dbContextProvider) : base(dbContextProvider) {
        }
        public async Task<IQueryable<T>> BulkInsertIntoTempTableAsync<T>(IEnumerable<T> entities) where T : class {
            var query = await Context.BulkInsertIntoTempTableAsync(entities, new SqlTempTableBulkInsertOptions() {
                PrimaryKeyCreation = PrimaryKeyCreation.None,
            });
            return query;
        }
        public IQueryable<T> FromLocalList<T>(IList<T> data) where T : class {
            return Context.FromLocalList(data);
        }
    }
3.3 defined in the DbContext
     public  dbquery <ImpTestTemp> {GET ImpTestTemps; SET;} 
==========================
4 can use class injection ITempTableRepository
        private readonly IObjectMapper _objectMapper;
        private readonly IRepository<Area, Guid> _area;
         private readonly ITempTableRepository _tempTableRepository;
        public AreaService(IRepository<Area, Guid> area, IObjectMapper objectMapper,
            ITempTableRepository tempTableRepository) {
            _area = area;
            _objectMapper = object mapper;
             _tempTableRepository = tempTableRepository;
        }
 
        public async Task<IEnumerable<GetAreaByParentIdOutput>> GetAreaByParentIdAsync(Guid? parentId) {
            var lst = new List<ImpTestTemp> {
                new ImpTestTemp { Id = 1, AreaId=new Guid("179A9943-EC6C-4923-8B75-00818198008E") ,Code = "123", Name = "yzn", Sorce=98.5m },
                new ImpTestTemp { Id = 2, AreaId= new Guid("48E8124C-789B-4158-BE9C-00507403DF7E"), Code = "456", Name = "seq" , Sorce=99.4m}
            };
             var queryList = await _tempTableRepository.BulkInsertIntoTempTableAsync (lst) ; // can use this data multiplexing method
            var queryList2 = _tempTableRepository.FromLocalList (lst); // This method is less data available 
            var efQuery = (from t in queryList
                           join a in _area.GetAll()
                           on t.AreaId equals a.Id
                           select new {
                               t.Code,
                               AreaCode = a.Code,
                               a.Name
                           }).ToList();
         retun null;          
        }
    }

Guess you like

Origin www.cnblogs.com/yangzn/p/11426205.html