Exemple #1
0
        public void TestRepositoryConstructorTest()
        {
            //1.准备工作
            IUnitOfWork    unitOfWork = new TestDBContext(); // TODO: 初始化为适当的值
            TestRepository target     = new TestRepository(unitOfWork);

            //
            bool isTrue = target.Context is TestDBContext;

            Assert.IsTrue(isTrue, "给定对象不能转换为指定的TestDBContext类型");
            isTrue = target.Context is BaseDBContext;
            Assert.IsTrue(isTrue, "给定对象不能转换为指定的BaseDBContext类型");
            TestModel test = new TestModel();

            target.Insert(test);
            TestDBContext testDBC = target.Context as TestDBContext;

            Assert.AreEqual(testDBC, target.Context);
            Assert.IsNotNull(testDBC.Tests.Find(test.testID));
            Assert.IsNull(testDBC.Tests.Find(0));
            BaseDBContext baseDBC = target.Context as BaseDBContext;

            Assert.IsNotNull(baseDBC.Set <TestModel>().Find(test.testID));
            Assert.IsNull(baseDBC.Set <TestModel>().Find(0));
            Assert.AreEqual(testDBC, target.Context);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="GenericRepository{TEntity}" /> class.
        /// </summary>
        /// <param name="unitOfWork">The unit of work.</param>
        /// <exception cref="System.ArgumentNullException">不能为null.</exception>
        /// <exception cref="System.ArgumentException">IUnitOfWork类型接口必须为可以转换为BaseDBContext类型的对象</exception>
        public GenericRepository(IUnitOfWork unitOfWork)
        {
            if (unitOfWork == null)
            {
                throw new ArgumentNullException("不能为null.");
            }

            _context = unitOfWork as BaseDBContext;
            if (_context == null)
            {
                throw new ArgumentException("IUnitOfWork类型接口必须为可以转换为BaseDBContext类型的对象");
            }

            this.dbSet = _context.Set <TEntity>();
        }
Exemple #3
0
        public virtual async Task AddRangeAsync(IEnumerable <TEntity> obj)
        {
            try
            {
                _context.ChangeTracker.LazyLoadingEnabled       = false;
                _context.ChangeTracker.QueryTrackingBehavior    = QueryTrackingBehavior.NoTracking;
                _context.ChangeTracker.AutoDetectChangesEnabled = false;

                await _context.BulkInsertAsync(obj.ToList());
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!_context.Set <TEntity>().Any())
                {
                    return;
                }
                else
                {
                    throw;
                }
            }
        }
Exemple #4
0
 /// <summary>
 /// 获取实体集合
 /// </summary>
 /// <returns></returns>
 public List <TEntity> GetAllList()
 {
     return(_dbContext.Set <TEntity>().ToList());
 }