Example #1
0
 public void Add(User model)
 {
     try
     {
         db.Set <User>().Add(model);
         db.SaveChanges();
     }
     catch (Exception ex)
     {
         var aa = ex.InnerException.Message;
     }
 }
Example #2
0
        public IEnumerable <T> GetData <T>() where T : BaseEntity
        {
            DemoContext demoContext = new DemoContext();
            var         data        = demoContext.Set <T>().ToList();

            return(data);
        }
Example #3
0
        private static Curso SoftDeleteCurso(int Id, DemoContext ctx)
        {
            try {
                var curso = ctx.Set <Curso>().Find(Id);
                if (curso == null)
                {
                    return(null);
                }

                ((ISoftDeleted)curso).Deleted = true;
                ctx.Set <Curso>().Attach(curso);                // agregarlo al changestracker
                ctx.Entry(curso).State = EntityState.Modified;
                return(curso);
            } catch (Exception) {
                return(null);
            }
        }
Example #4
0
        public static void AddPrimaryObjects(this DemoContext context)
        {
            if (context.Set <Domain.Models.PrimaryObject>().Any())
            {
                return;
            }

            var testModels = Enumerable.Range(1, 100)
                             .Select(i => new Domain.Models.PrimaryObject(Guid.Parse($"00000000-0000-0000-{i.ToString("0000")}-000000000000"))
            {
                Name        = $"Primary Object {i}",
                Description = $"This is a primary object {i}",
            })
                             .ToArray();

            context.Set <Domain.Models.PrimaryObject>().AddOrUpdate(s => s.Id, testModels);
        }
 protected ServiceBase(IDatabaseFactory databaseFactory, bool noTracking, bool lazyloading = true)
 {
     DatabaseFactory = databaseFactory;
     dbset           = DemoContext.Set <T>();
     if (noTracking)
     {
         dbset.AsNoTracking <T>();
     }
     demoContext.Configuration.LazyLoadingEnabled = lazyloading;
 }
Example #6
0
        public static void AddSecondaryObjects(this DemoContext context)
        {
            if (context.Set <Domain.Models.SecondaryObject>().Any())
            {
                return;
            }

            var testModels = context.Set <Domain.Models.PrimaryObject>()
                             .ToList()
                             .SelectMany(o => Enumerable.Range(1, 100)
                                         .Select(i => new Domain.Models.SecondaryObject(Guid.Parse($"00000000-0000-0000-{o.Id.ToString().Split('-')[3]}-{i.ToString("000000000000")}"))
            {
                Name             = $"Secondary Object {i}",
                Description      = $"This is a secondary object {i}",
                PrimaryObject    = o,
                PrimaryObject_Id = o.Id,
            }))
                             .ToArray();

            context.Set <Domain.Models.SecondaryObject>().AddOrUpdate(s => s.Id, testModels);
        }
Example #7
0
        private static Curso EditNoTrackingCurso(Curso curso, DemoContext ctx)
        {
            try {
                Curso existing = ctx.Set <Curso>().Find(curso.Id);
                if (existing == null)
                {
                    return(null);
                }

                ctx.Entry(existing).CurrentValues.SetValues(curso);
                return(existing);
            } catch (Exception e) {
                return(null);
            }
        }
Example #8
0
 public virtual async Task <IEnumerable <T> > ListAllAsync()
 {
     // SELECT ALL query
     return(await _demoContext.Set <T>().ToListAsync());
 }
Example #9
0
        /// <summary>
        /// 加载关联数据:即多连接
        /// 预加载:Eager loading   显式加载:Explicit loading   懒加载:Lazy loading(不推荐使用)
        /// </summary>
        /// <param name="args"></param>
        static void Main4(string[] args)
        {
            using var context = new DemoContext();

            #region 示例1

            /*var clubs = context.Clubs
             *              .Where(x => x.Id > 0)// IQueryable<Club>:只有 IQueryable<T> 才可以调用 Include 方法
             *               // 表示关联到 Clubs 表中的 League 属性
             *              .Include(x => x.League)// 返回值为 IIncludableQueryable<Club,League>
             *              .Include(x => x.Players)// 返回值为 IIncludableQueryable<Club,List<Player>>
             *                  // 还可以接着关联到 Player 类中的导航属性
             *                  .ThenInclude(y => y.Resume)// 返回值为 IIncludableQueryable<Club,Resume>
             *                  // 如果还想接着关联 Player 类中的导航属性,就不可以再 ThenInclude
             *                  // 因为此时再 ThenInclude 是基于 Resume 类而言的
             *              .Include(x => x.Players)
             *                  .ThenInclude(y => y.GamePlayers)// 返回值为 IIncludableQueryable<Club,List<GamePlayers>>
             *                      // 接着关联 GamePlayers 类中的 Game 导航属性
             *                      .ThenInclude(z => z.Game)// IIncludableQueryable<Club,Game>
             *              .FirstOrDefault();*/
            #endregion


            #region 示例2

            /*var info = context.Clubs
             *          .Where(x => x.Id > 0)
             *          .Select(x => new {
             *              x.Id,
             *              LeagueName = x.League.Name,
             *              x.Name,
             *              Players = x.Players
             *                          .Where(p => p.DateOfBirth > new DateTime(1990, 1, 1))
             *          }).ToList();
             * // context 无法追踪匿名类,只能追踪它识别类的变化
             * // 但是匿名类中 Players 属性,context 是可以追踪的,因为它可以识别到 Players 类
             *
             * foreach (var data in info) {
             *  foreach (var player in data.Players) {
             *      player.Name += "~";
             *  }
             * }
             *
             * context.SaveChanges();*/
            #endregion

            #region 示例3

            var info = context.Clubs.First();

            context.Entry(info)
            .Collection(x => x.Players)    // CollectionEntry<Club,Player>
            .Load();
            context.Entry(info)
            .Reference(x => x.League)    // ReferenceEntry<Club,League>
            .Load();

            context.Entry(info)
            .Collection(x => x.Players)
            .Query()
            .Where(x => x.DateOfBirth > new DateTime(1990, 1, 1))
            .Load();

            var data = context.Clubs
                       .Where(x => x.League.Name.Contains("e"))
                       .ToList();

            // player -- gameplayer -- game
            // 对于 context 对象中没有定义的数据,可以使用 Set<T> 方法获得其数据
            var gamePlayers = context.Set <GamePlayer>()
                              .Where(x => x.PlayerId > 1)
                              .ToList();

            #endregion
        }
Example #10
0
 public void Add(TEntity entity)
 {
     dbContext.Set <TEntity>().Add(entity);
     dbContext.SaveChanges();
 }
Example #11
0
 public RepositoryGeneric(DemoContext context)
 {
     Db    = context;
     DbSet = Db.Set <TEntity>();
 }
 public EfRepository(DemoContext dbContext)
 {
     _dbContext = dbContext;
     DbSet      = _dbContext.Set <T>();
 }
Example #13
0
 public async Task <IReadOnlyList <T> > GetAllAsync()
 {
     return(await DbContext.Set <T>().ToListAsync());
 }
Example #14
0
 protected ServiceBase(IDatabaseFactory databaseFactory)
 {
     DatabaseFactory = databaseFactory;
     dbset           = DemoContext.Set <T>();
 }
Example #15
0
 public IEnumerable <T> All()
 {
     return(_demoCtx.Set <T>());
 }
Example #16
0
 private IQueryable <T> ApplySpecification(ISpecification <T> spec)
 {
     return(SpecificationEvaluator <T> .GetQuery(_demoDbContext.Set <T>().AsQueryable(), spec));
 }
Example #17
0
 protected Repository(DemoContext context)
 {
     _entities = context.Set <TEntity>();
 }
Example #18
0
 public async Task <ActionResult> GetMovies()
 {
     return(Ok(await _context.Set <Movie>().ToListAsync()));
 }
 public CustomerService(DemoContext context = null)
 {
     _context = context ?? new DemoContext();
     _dbSet   = _context.Set <Customer>();
 }
Example #20
0
 public BaseRepository(DemoContext context)
 {
     this.context = context;
     this.dbSet   = context.Set <TEntity>();
 }
 public virtual T GetById(int id)
 {
     return(_dbContext.Set <T>().Find(id));
 }