Ejemplo n.º 1
0
        /// <summary>
        /// 自增更新表的某列字段 列 UPDATE User SET Name=Name+"zhang"   表达式为 context.User.RestValue(x=>x.Name==(x.Name+"zhang"))
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="updateValues"></param>
        /// <returns></returns>
        public static async Task <int> RestValueAsync <T>(this IQueryable <T> query, Expression <Func <T, bool> > updateValues) where T : class, new()
        {
            var context = EfCoreHelper.GetDbContext(query);

            var(sql, sp) = GetSqlUpdate(query, updateValues);
            return(await context.Database.ExecuteSqlCommandAsync(sql, sp));
        }
Ejemplo n.º 2
0
        public void Example_ctor_param_efcore()
        {
            IMapper mapper = new MapperConfiguration(builder =>
            {
                builder.CreateMap <MyEntity, MyModel>()
                .ForCtorParam("identifier", x => x.MapFrom(e => e.Id));

                //builder.CreateMap<MyEntity, SourceClass>()
                //    .ConstructUsing(x => new SourceClass(x.Id, x.Value));

                // Doesn't work.
                // ConstructUsing (Func<>): Could be ignore if the constructor's parameter names have the same naming.

                //builder.CreateMap<MyEntity, SourceClass>()
                //    .ConstructUsing((x, context) =>
                //    {
                //        return new SourceClass(x.Id, x.Value);
                //    });
            })
                             .CreateMapper();

            using EfContext db = EfCoreHelper.CreateDbContext <EfContext>();

            List <MyModel> source = db.Entities
                                    .ProjectTo <MyModel>(mapper.ConfigurationProvider)
                                    .ToList();

            source.Should().OnlyHaveUniqueItems(e => e.Identifier);
            source.Should().OnlyHaveUniqueItems(e => e.Value);
            source.Should().HaveCount(2);
        }
Ejemplo n.º 3
0
        public EfCoreLazyLoading(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
        {
            _demoDbContext = EfCoreHelper.CreateDbContext <MyDbContext>(configure =>
            {
                configure.UseLoggerFactory(LoggerFactory);
                configure.UseLazyLoadingProxies();
            });

            _mapper = new MapperConfiguration(
                builder =>
            {
                builder.CreateMap <Domaine, DomaineDto>()
                .ForMember(x => x.TotalArticles, x => x.MapFrom(e => e.Articles.Count()));

                builder.CreateMap <Article, ArticleDto>();
            })
                      .CreateMapper();
        }