Exemplo n.º 1
0
        public void Update(VideoAddDto dto)
        {
            using (YersDbContext ctx = new YersDbContext())
            {
                BaseService <VideoEntity> bs
                    = new BaseService <VideoEntity>(ctx);

                bool exists = bs.GetAll().Any(u => u.Title == dto.Title && u.Id != dto.Id);

                if (exists)
                {
                    throw new ArgumentException("该视频已经存在:" + dto.Title);
                }
                var entity = dto.EntityMap();

                DbEntityEntry <VideoEntity> entry = ctx.Entry <VideoEntity>(entity);
                entry.State = EntityState.Modified;
                ctx.SaveChanges();

                //ctx.Videos.Attach(entity);
                //ctx.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);

                //ctx.SaveChanges();
            }
        }
Exemplo n.º 2
0
        public long AddNew(VideoAddDto dto)
        {
            VideoEntity videoEntity = dto.EntityMap();

            videoEntity.CreateDateTime = DateTime.Now;
            using (YersDbContext ctx = new YersDbContext())
            {
                BaseService <VideoEntity> bs
                    = new BaseService <VideoEntity>(ctx);

                if (bs.GetAll().Any(m => m.Title == videoEntity.Title))
                {
                    throw new ArgumentException("该视频标题已存在,请检查");
                }

                ctx.Videos.Add(videoEntity);

                ctx.SaveChanges();

                return(videoEntity.Id);
            }
        }