public IList <YorumDto> GetList(int yaziId) { List <YorumDto> yorumList = new List <YorumDto>(); var yorumRep = new EfRepositoryBase <Yorum, YilmazCoderContext>(); var kulRep = new EfRepositoryBase <Kullanici, YilmazCoderContext>(); var yorumLst = (from yorum in yorumRep.GetList(x => x.YaziId == yaziId) join kul in kulRep.GetList() on yorum.KullaniciId equals kul.Id select new { yorum = yorum, kullanici = kul }).ToList(); foreach (var item in yorumLst) { YorumDto y = new YorumDto(); y.Id = item.yorum.Id; y.Aciklama = item.yorum.Aciklama; y.KayitTarihi = item.yorum.KayitTarihi; y.KullaniciAdi = item.kullanici.KullaniciAdi; y.KullaniciId = item.kullanici.Id; y.YaziId = item.yorum.YaziId; y.KullaniciResmi = item.kullanici.Resim; yorumList.Add(y); } return(yorumList); }
public YaziDto GetById(int yaziId) { using (YilmazCoderContext context = new YilmazCoderContext()) { YaziDto yaziDto = new YaziDto(); var yaziRep = new EfRepositoryBase <Yazi, YilmazCoderContext>(); var kullaniciRep = new EfRepositoryBase <Kullanici, YilmazCoderContext>(); var yorumRep = new EfRepositoryBase <Yorum, YilmazCoderContext>(); var begeniRep = new EfRepositoryBase <Begeni, YilmazCoderContext>(); var kategoriRep = new EfRepositoryBase <Kategori, YilmazCoderContext>(); var yazilist = (from yazi in context.Yazi.Where(x => x.Id == yaziId) join kullanici in context.Kullanici on yazi.KullaniciId equals kullanici.Id select new YaziDto { Id = yazi.Id, KategoriId = yazi.KategoriId, KategoriAdi = context.Kategori.Where(x => x.Id == yazi.KategoriId).FirstOrDefault().Adi, KullaniciAdi = kullanici.KullaniciAdi, KullaniciId = kullanici.Id, KullaniciResmi = kullanici.Resim, YaziBaslik = yazi.YaziBaslik, YaziIcerik = yazi.YaziIcerik, YaziTarih = yazi.YaziTarih, YaziKapakResim = yazi.YaziKapakResim, BegeniSayisi = context.Begeni.Where(x => x.YaziId == yazi.Id).Count(), YorumSayisi = context.Yorum.Where(x => x.YaziId == yazi.Id).Count(), }).FirstOrDefault(); return(yazilist); } }
private EfRepositoryBase <T> Get <T>() where T : class { if (_dictionary.ContainsKey(typeof(T))) { return(_dictionary[typeof(T)] as EfRepositoryBase <T>); } var efr = new EfRepositoryBase <T>(_context); Put(efr); return(efr); }
// GET: Home public ActionResult Index(int page = 0, int size = 15) { var context = new SimpleDbContextProvider <CodeFirstDbContext>(new UserDbContext()); var respository = new EfRepositoryBase <CodeFirstDbContext, User, long>(context); var clsResp = new EfRepositoryBase <CodeFirstDbContext, MClass, string>(context); // sql // var users = // respository.SqlQuery( // "select top 15 [UserID] as [Id],[Email],[TrueName],[AddedAt],[Role],[Status] from [TU_User] where [Role]=@role order by AddedAt desc", // new SqlParameter("@role", (byte)4)); var managers = clsResp.Query( c => c.ClassManagerID.HasValue && c.ClassManagerID > 0 && c.Status == (byte)0) .Include(c => c.Manager) .Include(c => c.Agency); Mapper.CreateMap <MClass, ClassDto>() .ForMember(s => s.Manager, d => d.MapFrom(c => c.Manager.TrueName)) .ForMember(s => s.AgencyName, d => d.MapFrom(c => c.Agency.AgencyName)); var cls = Mapper.Map <List <ClassDto> >(managers); // inner join // var users = respository // .Query(u => u.Status == 0) // .Join(managers, u => u.Id, m => m.ClassManagerID, (e, o) => e) // .Distinct() // .OrderByDescending(u => u.AddedAt) // .Skip(page * size) // .Take(size) // .ToList(); // exists // var users = respository // .Query(u => managers.Any(m => m.ClassManagerID == u.Id)) // .OrderByDescending(u => u.AddedAt) // .Skip(page * size) // .Take(size) // .ToList(); // LEFT OUTER JOIN // var users = clsResp.Query( // c => c.ClassManagerID.HasValue && c.ClassManagerID > 0 && c.Status == (byte)0) // .Select(c => c.Manager).Distinct().ToList(); return(View(cls)); }
public void Delete() { User user = new User { Id = 10 //不能用非主键这样删除 //UserAccount = "123", }; _repository.Delete(user); _repository.SaveChanges(); using (var newContext = new TestContext()) { var repository = new EfRepositoryBase <User>(newContext); var userInDb = repository.FirstOrDefault(c => c.UserAccount == "123"); Assert.True(userInDb == null); } }
// GET: Home public ActionResult Index(int page = 0, int size = 15) { var context = new SimpleDbContextProvider<CodeFirstDbContext>(new UserDbContext()); var respository = new EfRepositoryBase<CodeFirstDbContext, User, long>(context); var clsResp = new EfRepositoryBase<CodeFirstDbContext, MClass, string>(context); // sql // var users = // respository.SqlQuery( // "select top 15 [UserID] as [Id],[Email],[TrueName],[AddedAt],[Role],[Status] from [TU_User] where [Role]=@role order by AddedAt desc", // new SqlParameter("@role", (byte)4)); var managers = clsResp.Query( c => c.ClassManagerID.HasValue && c.ClassManagerID > 0 && c.Status == (byte) 0) .Include(c => c.Manager) .Include(c => c.Agency); Mapper.CreateMap<MClass, ClassDto>() .ForMember(s => s.Manager, d => d.MapFrom(c => c.Manager.TrueName)) .ForMember(s => s.AgencyName, d => d.MapFrom(c => c.Agency.AgencyName)); var cls = Mapper.Map<List<ClassDto>>(managers); // inner join // var users = respository // .Query(u => u.Status == 0) // .Join(managers, u => u.Id, m => m.ClassManagerID, (e, o) => e) // .Distinct() // .OrderByDescending(u => u.AddedAt) // .Skip(page * size) // .Take(size) // .ToList(); // exists // var users = respository // .Query(u => managers.Any(m => m.ClassManagerID == u.Id)) // .OrderByDescending(u => u.AddedAt) // .Skip(page * size) // .Take(size) // .ToList(); // LEFT OUTER JOIN // var users = clsResp.Query( // c => c.ClassManagerID.HasValue && c.ClassManagerID > 0 && c.Status == (byte)0) // .Select(c => c.Manager).Distinct().ToList(); return View(cls); }
public void Test_insert_update_attach_success() { User user = new User { UserName = "******", UserAccount = "1233123213123", UserPassWord = "******", CertificateNo = "123131", Mobile = 13313331333, IsVerify = IsVerifyEnum.可用 }; _repository.Insert(user); _repository.SaveChanges(); var newUserId = user.Id; Assert.True(newUserId > 0); //update using (var newContext = new TestContext()) { var repository = new EfRepositoryBase <User>(newContext); var userInDb = repository.FirstOrDefault(c => c.Id == newUserId); userInDb.UserName = "******"; repository.Update(userInDb); repository.SaveChanges(); } //assert using (var newContext = new TestContext()) { var repository = new EfRepositoryBase <User>(newContext); var userInDb = repository.FirstOrDefault(c => c.Id == newUserId); Assert.True(userInDb.UserName == "lisi"); } _repository.Delete(user); _repository.SaveChanges(); }
public List <Yetki> YetkiList(Kullanici kullanici) { List <Yetki> ylist = new List <Yetki>(); var yetkiRep = new EfRepositoryBase <Yetki, YilmazCoderContext>(); var kullaniciYetkiRep = new EfRepositoryBase <KullaniciYetki, YilmazCoderContext>(); var yazilist = yetkiRep.GetList().Join(kullaniciYetkiRep.GetList(x => x.KullaniciId == kullanici.Id), yetki => yetki.Id, kullaniciYetki => kullaniciYetki.KullaniciId, (yetki, kullaniciYetki) => new { Yetki = yetki, KullaniciYetki = kullaniciYetki }).ToList(); foreach (var item in yazilist) { Yetki y = new Yetki(); y.Id = item.Yetki.Id; y.Adi = item.Yetki.Adi; ylist.Add(y); } return(ylist); }
public void Test_insert_getbyid_table_tablenotracking_delete_success() { User user = new User { UserName = "******", UserAccount = "1233123213123", UserPassWord = "******", CertificateNo = "123131", Mobile = 13313331333, IsVerify = IsVerifyEnum.可用 }; _repository.Insert(user); _repository.SaveChanges(); var newUserId = user.Id; Assert.True(newUserId > 0); //声明新的Context,不然查询直接由DbContext返回而不经过数据库 using (var newContext = new TestContext()) { var repository = new EfRepositoryBase <User>(newContext); var userInDb = repository.FirstOrDefault(c => c.Id == newUserId); Assert.True(user.UserName == userInDb.UserName); } Assert.True(_context.Entry(user).State == EntityState.Unchanged); _repository.Delete(user); _repository.SaveChanges(); using (var newContext = new TestContext()) { var repository = new EfRepositoryBase <User>(newContext); var userInDb = repository.FirstOrDefault(c => c.Id == newUserId); Assert.True(userInDb == null); } }
private void Put <T>(EfRepositoryBase <T> item) where T : class { _dictionary[typeof(T)] = item; }
private readonly EfRepositoryBase <User> _repository;//用具体的泛型类型进行测试,这个不影响对EFRepository测试的效果 public EfRepositoryBaseTest() { _context = new TestContext(); _repository = new EfRepositoryBase <User>(_context); }
public RepositoryTests() { _recipeRepository = A.Fake <EfRepositoryBase <Recipe, long> >(); }