public void ODT_EagerLoad_CascadeAndMulti() { using (RF.TransactionScope(UnitTestEntityRepositoryDataProvider.DbSettingName)) { var so = new SectionOwner { Name = "SO1" }; RF.Save(so); RF.Save(new Book { Name = "book", ChapterList = { new Chapter { Name = "chapter", SectionList = { new Section { Name = "Section1", SectionOwner = so }, new Section { Name = "Section2", SectionOwner = so }, } } } }); var list = RF.ResolveInstance <SectionRepository>().GetBy(new ODataQueryCriteria { Expand = "Chapter.Book,SectionOwner" }) as SectionList; Assert.AreEqual(list.Count, 2); Assert.IsTrue(list[0].FieldExists(Section.ChapterProperty)); Assert.IsTrue(list[1].FieldExists(Section.ChapterProperty)); Assert.IsTrue(list[0].Chapter == list[1].Chapter); Assert.IsTrue(list[0].FieldExists(Section.SectionOwnerProperty)); Assert.IsTrue(list[1].FieldExists(Section.SectionOwnerProperty)); Assert.IsTrue(list[0].SectionOwner == list[1].SectionOwner); Assert.IsTrue(list[0].Chapter.FieldExists(Chapter.BookProperty), "Chapter.Book 也已经加载"); } }
public void ORM_AggtSQL_LoadEntities() { using (RF.TransactionScope(UnitTestEntityRepositoryDataProvider.DbSettingName)) { var so = new SectionOwner(); RF.Save(so); var book = CreateAggtBook(so); RF.Save(book); var api = AggregateSQL.Instance; var loadOptions = api .BeginLoadOptions<Chapter>().LoadChildren(c => c.SectionList) .Order<Section>().By(p => p.Name) //.Continue<PBSProperty>() .LoadFK(p => p.SectionOwner); var sql = api.GenerateQuerySQL(loadOptions, book.Id); var sql2 = api.GenerateQuerySQL(loadOptions, string.Format("Chapter.BookId = {0}", book.Id)); Assert.AreEqual(sql, sql2); //聚合加载整个对象树。 var entities = new ChapterList(); api.LoadEntities(entities, sql, loadOptions); //无懒加载测试。 var count = Logger.DbAccessedCount; foreach (Chapter chapter in entities) { foreach (Section section in chapter.SectionList) { var so2 = section.SectionOwner; } } Assert.IsTrue(Logger.DbAccessedCount == count, "由于数据已经全部加载完成,所以这里不会发生懒加载。"); } }
public void ORM_LinqQuery_WhereChildren_All_Any() { var repo = RF.Concrete<BookRepository>(); using (RF.TransactionScope(repo)) { var so = new SectionOwner { Name = "huqf" }; RF.Save(so); repo.Save(new Book { Name = "1", ChapterList = { new Chapter { SectionList = { new Section { SectionOwner = so }, } }, new Chapter { SectionList = { new Section { SectionOwner = so }, } } } }); repo.Save(new Book { Name = "2", ChapterList = { new Chapter { SectionList = { new Section { SectionOwner = so }, new Section { }, } } } }); repo.Save(new Book { Name = "3", }); repo.Save(new Book { Name = "4", ChapterList = { new Chapter { SectionList = { new Section { SectionOwner = so }, } }, new Chapter { SectionList = { new Section { },//not match } } } }); var list = repo.LinqGetIfChildren_All_Any(); Assert.IsTrue(list.Count == 3); Assert.IsTrue(list[0].Name == "1"); Assert.IsTrue(list[1].Name == "2"); Assert.IsTrue(list[2].Name == "3"); } }
private static Book CreateAggtBook(SectionOwner so) { var book = new Book { ChapterList = { new Chapter { SectionList = { new Section{ SectionOwner = so }, new Section{ SectionOwner = so }, new Section{ SectionOwner = so }, new Section{ SectionOwner = so }, } }, new Chapter { SectionList = { new Section{ SectionOwner = so }, new Section{ SectionOwner = so }, new Section{ SectionOwner = so }, new Section{ SectionOwner = so }, } }, new Chapter { SectionList = { new Section{ SectionOwner = so }, new Section{ SectionOwner = so }, new Section{ SectionOwner = so }, new Section{ SectionOwner = so }, } } } }; return book; }
public void ORM_Query_EagerLoad_IPropertyQuery() { var repo = RF.Concrete<BookRepository>(); using (RF.TransactionScope(repo)) { var so = new SectionOwner(); RF.Save(so); var book = CreateAggtBook(so); RF.Save(book); //查询的数据访问测试。 var oldCount = Logger.DbAccessedCount; var all = repo.GetWithEager(); var newCount = Logger.DbAccessedCount; Assert.IsTrue(newCount - oldCount == 4, "应该只进行了 4 次数据库查询。"); //无懒加载测试。 foreach (Book book2 in all) { foreach (Chapter chapter in book2.ChapterList) { foreach (Section section in chapter.SectionList) { var so2 = section.SectionOwner; } } } Assert.IsTrue(Logger.DbAccessedCount == newCount, "由于数据已经全部加载完成,所以这里不会发生懒加载。"); } }
private static void AddBookForAggtQuery(BookRepository repo) { var so = new SectionOwner { Name = "huqf" }; RF.Save(so); var category = new BookCategory { Name = "category" }; RF.Save(category); repo.Save(new Book { Name = "1",//not match BookCategory = category, ChapterList = { new Chapter { Name = "chapterNeed", SectionList = { new Section { Name = "section need", SectionOwner = so }, new Section { Name = "need section", SectionOwner = so } } }, new Chapter { Name = "1.2"} } }); repo.Save(new Book { Name = "2", //BookCategory = category,//not match ChapterList = { new Chapter { Name = "chapterNeed", SectionList = { new Section { Name = "section need", SectionOwner = so }, new Section { Name = "need section", SectionOwner = so } } }, new Chapter { Name = "1.2"}, new Chapter { Name = "1.3"}, } }); repo.Save(new Book { Name = "3", BookCategory = category, ChapterList = { new Chapter { Name = "chapterNeed", SectionList = { new Section { Name = "section need", SectionOwner = so }, new Section { Name = "need section", SectionOwner = so } } }, //new Chapter { Name = "1.2"}//not match } }); repo.Save(new Book { Name = "4", BookCategory = category, ChapterList = { new Chapter { Name = "4.1",//not match SectionList = { new Section { Name = "section need", SectionOwner = so }, new Section { Name = "need section", SectionOwner = so } } }, new Chapter { Name = "1.2"}, } }); repo.Save(new Book { Name = "5", BookCategory = category, ChapterList = { new Chapter { Name = "chapterNeed", SectionList = { new Section { Name = "section need", SectionOwner = so }, new Section { Name = "section", SectionOwner = so }//not match } }, new Chapter { Name = "1.2"}, } }); repo.Save(new Book { Name = "6", BookCategory = category, ChapterList = { new Chapter { Name = "chapterNeed", SectionList = { new Section { Name = "section need", SectionOwner = so }, new Section { Name = "need section" }//not match } }, new Chapter { Name = "1.2"}, } }); //添加 10 个满足条件的数据。 for (int i = 11; i <= 20; i++) { repo.Save(new Book { Name = i.ToString(), BookCategory = category, ChapterList = { new Chapter { Name = "chapterNeed", SectionList = { new Section { Name = "section need", SectionOwner = so }, new Section { Name = "need section", SectionOwner = so } } }, new Chapter { Name = "1.2"}, new Chapter { Name = "1.3"}, } }); } }
public void ORM_LinqQuery_WhereChildren_Any_SectionAndOwner() { var repo = RF.Concrete<BookRepository>(); using (RF.TransactionScope(repo)) { var so = new SectionOwner { Name = "huqf" }; RF.Save(so); var category = new BookCategory { Name = "category" }; RF.Save(category); repo.Save(new Book { Name = "1", BookCategory = category, ChapterList = { new Chapter { SectionList = { new Section { Name = "need", SectionOwner = so } } }, } }); repo.Save(new Book { Name = "2", BookCategory = category, ChapterList = { new Chapter { SectionList = { new Section { Name = "need too" }// sectionOwner not match } }, } }); repo.Save(new Book { Name = "3", //BookCategory = category,// not match ChapterList = { new Chapter { SectionList = { new Section { Name = "need", SectionOwner = so } } }, } }); repo.Save(new Book { Name = "4", ChapterList = { new Chapter { SectionList = { new Section { Name = "not contained" }//section name not match. } }, } }); var list = repo.LinqGetIfChildrenExistsSectionAndOwner(category.Name, "need", so.Name); Assert.IsTrue(list.Count == 1); Assert.IsTrue(list[0].Name == "1"); } }
public void ORM_LinqQuery_WhereRefAndWhereRef() { var repo = RF.Concrete<SectionRepository>(); using (RF.TransactionScope(repo)) { var sectionOwner = new SectionOwner { Name = "SO" }; RF.Save(sectionOwner); RF.Save(new Book { Name = "1", ChapterList = { new Chapter { Name = "1.1", SectionList = { new Section { Name = "1.1.1", SectionOwner = sectionOwner }, new Section { Name = "1.1.2", SectionOwner = sectionOwner }, new Section { Name = "1.1.3" }, } }, new Chapter { Name = "1.2", SectionList = { new Section { Name = "1.1.1", SectionOwner = sectionOwner }, new Section { Name = "1.1.2" }, new Section { Name = "1.1.3" }, new Section { Name = "1.1.4" }, } }, } }); RF.Save(new Book { Name = "2", ChapterList = { new Chapter { Name = "2.1", SectionList = { new Section { Name = "2.1.1", SectionOwner = sectionOwner }, new Section { Name = "2.1.2", SectionOwner = sectionOwner }, new Section { Name = "2.1.3" }, } }, } }); var list = repo.GetByBookNameOwner("1", 1); Assert.IsTrue(list.Count == 3); list = repo.GetByBookNameOwner("1", 2); Assert.IsTrue(list.Count == 4); list = repo.GetByBookNameOwner("2", 0); Assert.IsTrue(list.Count == 3); } }
public int IndexOf(SectionOwner entity) { return base.IndexOf(entity); }
public void ORM_AggtSQL_LoadReferenceEntities() { using (RF.TransactionScope(UnitTestEntityRepositoryDataProvider.DbSettingName)) { var so = new SectionOwner(); RF.Save(so); var book = CreateAggtBook(so); RF.Save(book); var api = AggregateSQL.Instance; var loadOptions = api .BeginLoadOptions<Book>() .LoadChildren(pp => pp.ChapterList) .Continue<Chapter>().LoadChildren(c => c.SectionList) .Order<Section>().By(v => v.SectionOwner.Name) .LoadFK(v => v.SectionOwner); var sql = api.GenerateQuerySQL(loadOptions, book.Id); //聚合加载整个对象树。 var entities = new BookList(); api.LoadEntities(entities, sql, loadOptions); //无懒加载测试。 var count = Logger.DbAccessedCount; foreach (Book book2 in entities) { foreach (Chapter chapter in book2.ChapterList) { foreach (Section section in chapter.SectionList) { var so2 = section.SectionOwner; } } } Assert.IsTrue(Logger.DbAccessedCount == count, "由于数据已经全部加载完成,所以这里不会发生懒加载。"); } }
public bool Contains(SectionOwner entity) { return base.Contains(entity); }
public void Add(SectionOwner entity) { base.Add(entity); }
public bool Remove(SectionOwner entity) { return base.Remove(entity); }
public void Insert(int index, SectionOwner entity) { base.Insert(index, entity); }
public void ODT_EagerLoad_Multi() { using (RF.TransactionScope(UnitTestEntityRepositoryDataProvider.DbSettingName)) { var so = new SectionOwner { Name = "SO1" }; RF.Save(so); RF.Save(new Book { Name = "book", ChapterList = { new Chapter { Name = "chapter", SectionList = { new Section { Name = "Section1", SectionOwner = so }, new Section { Name = "Section2", SectionOwner = so }, } } } }); var list = RF.Concrete<SectionRepository>().GetBy(new ODataQueryCriteria { Expand = "Chapter,SectionOwner" }) as SectionList; Assert.AreEqual(list.Count, 2); Assert.IsTrue(list[0].FieldExists(Section.ChapterProperty)); Assert.IsTrue(list[1].FieldExists(Section.ChapterProperty)); Assert.IsTrue(list[0].Chapter == list[1].Chapter); Assert.IsTrue(list[0].FieldExists(Section.SectionOwnerProperty)); Assert.IsTrue(list[1].FieldExists(Section.SectionOwnerProperty)); Assert.IsTrue(list[0].SectionOwner == list[1].SectionOwner); } }