public void FindById() { ConsoleTitleUtil.ShowTestTitle("FindById"); // 基类TDataRoot,子类TPost和TTopic //子类简单查询 TPost post = TPost.findById(3) as TPost; Assert.IsNotNull(post); Assert.AreEqual(3, post.Id); Console.WriteLine("Id:{0} Title:{1}", post.Id, post.Title); TPost post2 = TPost.findById(11) as TPost; Assert.IsNotNull(post2); //多态查询:正确判断结果是相应子类 TDataRoot root = TDataRoot.findById(6); Assert.IsNotNull(root); Assert.AreEqual(6, root.Id); Assert.AreEqual(typeof(TTopic), root.GetType()); //针对属性的多态查询 Assert.AreEqual(typeof(TTopicCategory), root.Category.GetType()); }
public IActionResult Edit(CPostViewModel post) { try { if (post != null) { TPost P_被修改 = context.TPosts.FirstOrDefault(n => n.FPostId == post.FPostId); if (P_被修改 != null) { P_被修改.FTitle = post.FTitle; P_被修改.FDescription = post.FDescription; P_被修改.FPeople = post.FPeople; P_被修改.FPostAddress = post.FPostAddress; P_被修改.FPostCity = post.FPostCity; P_被修改.FPostDistrict = post.FPostDistrict; P_被修改.FPostTime = post.FPostTime; P_被修改.FSportName = post.FSportName; P_被修改.FSystemTime = DateTime.Now.ToString(); P_被修改.FPostCheck = post.FPostCheck; P_被修改.FLikeCount = post.FLikeCount.ToString(); //TJoinPerson joinPerson = new TJoinPerson(); //參加人 //joinPerson.FUserId = post.FUserId; //joinPerson.FPostId = post.FPostId; if (post.image != null) { foreach (var i in post.image) { string PhotoName = Guid.NewGuid().ToString() + ".jpg"; using (var photo = new FileStream(iv_host.ContentRootPath + @"/wwwroot/img/" + PhotoName, FileMode.Create)) { i.CopyTo(photo); } TPostPhoto tp = new TPostPhoto(); tp.FPostPhoto = "~/img/" + PhotoName; tp.FPostId = post.FPostId; context.TPostPhotos.Add(tp); context.SaveChanges(); } } context.SaveChanges(); } } return(RedirectToAction("Post_Index", "CPost")); } catch { return(RedirectToAction("Post_Index", "CPost")); } }
public void Delete() { ConsoleTitleUtil.ShowTestTitle("Delete"); //子类删除:删除子类的同时,也要删除父类中的数据 TPost post = TPost.findById(11) as TPost; Assert.IsNotNull(post); db.delete(post); post = TPost.findById(11) as TPost; Assert.IsNull(post); //父类删除:同时删除子类 TDataRoot.delete(13); Assert.IsNull(TDataRoot.findById(13)); }
public void Update() { ConsoleTitleUtil.ShowTestTitle("Update"); //子类更新:父类相应属性也要更新 TPost post = TPost.findById(3) as TPost; post.Title = "**更新之后的子类"; db.update(post); string sql = "select title from Tdataroot where id=3"; string title = wojilu.Data.EasyDB.ExecuteScalar(sql, wojilu.Data.DbContext.getConnection(post.GetType())) as string; Assert.IsNotNull(title); Assert.AreEqual(post.Title, title); //父类更新:其实是真实的底层子类更新,同上 }
public void A_Insert() { wojiluOrmTestInit.ClearLog(); wojiluOrmTestInit.InitMetaData(); ConsoleTitleUtil.ShowTestTitle( "Insert" ); //wojilu.file.Delete( "log.txt" ); // �˴�Ӧ���������������� // Ȼ��������������ݣ�ͬʱ����insert sql������Id��ֵ�� // �ܹ����46������ for (int i = 0; i < 20; i++) { // �ڲ������ݵ�ʱ��̬����û���ر���Ҫע��� TPostCategory pcat = new TPostCategory(); pcat.Name = "post���ӷ���"; pcat.Hits = new Random().Next( 1, 100 ); db.insert( pcat ); Assert.Greater( pcat.Id, 0 ); TTopicCategory tcat = new TTopicCategory(); tcat.Name = "topic�������"; tcat.ReplyCount = new Random().Next( 1, 200 ); db.insert( tcat ); Assert.Greater( tcat.Id, 0 ); TPost post = new TPost(); post.Title = "post_34��������Ա�����º�"; post.Body = "ϣ����Ĺ�ȥ�ķ����ִ�Y��¯�ĒY���_��"; post.Uid = "����"; post.Category = pcat; // ��̬������� post.Hits = new Random().Next(); db.insert( post ); Assert.Greater( post.Id, 0 ); TTopic topic = new TTopic(); topic.Title = "topic_������������"; topic.Body = "�����ƺ�����֣��������Ȼ�Dz���˵�ġ����Ǻ�������˵�����ܡ������һ�����䡣"; topic.Uid = "����"; topic.Category = tcat; topic.Hits = new Random().Next( 34, 10039343 ); topic.ReplyCount = 3; db.insert( topic ); Assert.Greater( topic.Id, 0 ); } for (int i = 0; i < 3; i++) { // ����������ӣ�����������ƺ����ӷ���������ͬ������������Ե�ʱ���Ƿ�Ҳ�ڶ�̬��ѯ����� TTopicCategory tcatfake = new TTopicCategory(); tcatfake.Name = "zzTopic���ӷ���"; tcatfake.ReplyCount = new Random().Next( 1, 200 ); db.insert( tcatfake ); Assert.Greater( tcatfake.Id, 0 ); TTopic topicfake = new TTopic(); topicfake.Title = "zzTopic������������"; topicfake.Body = "�����ƺ�����֣��������Ȼ�Dz���˵�ġ����Ǻ�������˵�����ܡ������һ�����䡣"; topicfake.Uid = "����"; topicfake.Category = tcatfake; topicfake.Hits = new Random().Next( 34, 10039343 ); topicfake.ReplyCount = 3; db.insert( topicfake ); Assert.Greater( topicfake.Id, 0 ); // ֱ����Ӹ���ľ������� TCategory category = new TCategory(); category.Name = "post���ӷ���"; db.insert( category ); TDataRoot root = new TDataRoot(); root.Title = "zzParent���Ǹ���֮init��ʼ��"; root.Body = "���������֮init��ʼ��"; root.Category = category; db.insert( root ); } insertAbstractTest(); }
public void A_Insert() { wojiluOrmTestInit.ClearLog(); wojiluOrmTestInit.InitMetaData(); ConsoleTitleUtil.ShowTestTitle("Insert"); //wojilu.file.Delete( "log.txt" ); // 此处应该先向基类添加数据 // 然后向子类添加数据(同时调整insert sql,插入Id的值) // 总共添加46条数据 for (int i = 0; i < 20; i++) { // 在插入数据的时候,多态关联没有特别需要注意的 TPostCategory pcat = new TPostCategory(); pcat.Name = "post帖子分类"; pcat.Hits = new Random().Next(1, 100); db.insert(pcat); Assert.Greater(pcat.Id, 0); TTopicCategory tcat = new TTopicCategory(); tcat.Name = "topic主题分类"; tcat.ReplyCount = new Random().Next(1, 200); db.insert(tcat); Assert.Greater(tcat.Id, 0); TPost post = new TPost(); post.Title = "post_34名美国议员联名致函"; post.Body = "希腊深化的过去的发恩持大扽肯炉衬扽拉歘称"; post.Uid = "张三"; post.Category = pcat; // 多态关联添加 post.Hits = new Random().Next(); db.insert(post); Assert.Greater(post.Id, 0); TTopic topic = new TTopic(); topic.Title = "topic_我是主题帖子"; topic.Body = "标题似乎很奇怪,秘密嘛,自然是不能说的。于是乎“不能说的秘密”便成了一个病句。"; topic.Uid = "李四"; topic.Category = tcat; topic.Hits = new Random().Next(34, 10039343); topic.ReplyCount = 3; db.insert(topic); Assert.Greater(topic.Id, 0); } for (int i = 0; i < 3; i++) { // 添加主题帖子,让其分类名称和帖子分类名称相同,便于下面测试的时候看是否也在多态查询结果中 TTopicCategory tcatfake = new TTopicCategory(); tcatfake.Name = "zzTopic帖子分类"; tcatfake.ReplyCount = new Random().Next(1, 200); db.insert(tcatfake); Assert.Greater(tcatfake.Id, 0); TTopic topicfake = new TTopic(); topicfake.Title = "zzTopic我是主题帖子"; topicfake.Body = "标题似乎很奇怪,秘密嘛,自然是不能说的。于是乎“不能说的秘密”便成了一个病句。"; topicfake.Uid = "李四"; topicfake.Category = tcatfake; topicfake.Hits = new Random().Next(34, 10039343); topicfake.ReplyCount = 3; db.insert(topicfake); Assert.Greater(topicfake.Id, 0); // 直接添加父类的具体数据 TCategory category = new TCategory(); category.Name = "post帖子分类"; db.insert(category); TDataRoot root = new TDataRoot(); root.Title = "zzParent我是父类之init初始化"; root.Body = "父类的内容之init初始化"; root.Category = category; db.insert(root); } insertAbstractTest(); }
public void A_Insert() { wojiluOrmTestInit.ClearLog(); wojiluOrmTestInit.InitMetaData(); ConsoleTitleUtil.ShowTestTitle( "Insert" ); //wojilu.file.Delete( "log.txt" ); // 此处应该先向基类添加数据 // 然后向子类添加数据(同时调整insert sql,插入Id的值) // 总共添加46条数据 for (int i = 0; i < 20; i++) { // 在插入数据的时候,多态关联没有特别需要注意的 TPostCategory pcat = new TPostCategory(); pcat.Name = "post帖子分类"; pcat.Hits = new Random().Next( 1, 100 ); db.insert( pcat ); Assert.Greater( pcat.Id, 0 ); TTopicCategory tcat = new TTopicCategory(); tcat.Name = "topic主题分类"; tcat.ReplyCount = new Random().Next( 1, 200 ); db.insert( tcat ); Assert.Greater( tcat.Id, 0 ); TPost post = new TPost(); post.Title = "post_34名美国议员联名致函"; post.Body = "希腊深化的过去的发恩持大扽肯炉衬扽拉歘称"; post.Uid = "张三"; post.Category = pcat; // 多态关联添加 post.Hits = new Random().Next(); db.insert( post ); Assert.Greater( post.Id, 0 ); TTopic topic = new TTopic(); topic.Title = "topic_我是主题帖子"; topic.Body = "标题似乎很奇怪,秘密嘛,自然是不能说的。于是乎“不能说的秘密”便成了一个病句。"; topic.Uid = "李四"; topic.Category = tcat; topic.Hits = new Random().Next( 34, 10039343 ); topic.ReplyCount = 3; db.insert( topic ); Assert.Greater( topic.Id, 0 ); } for (int i = 0; i < 3; i++) { // 添加主题帖子,让其分类名称和帖子分类名称相同,便于下面测试的时候看是否也在多态查询结果中 TTopicCategory tcatfake = new TTopicCategory(); tcatfake.Name = "zzTopic帖子分类"; tcatfake.ReplyCount = new Random().Next( 1, 200 ); db.insert( tcatfake ); Assert.Greater( tcatfake.Id, 0 ); TTopic topicfake = new TTopic(); topicfake.Title = "zzTopic我是主题帖子"; topicfake.Body = "标题似乎很奇怪,秘密嘛,自然是不能说的。于是乎“不能说的秘密”便成了一个病句。"; topicfake.Uid = "李四"; topicfake.Category = tcatfake; topicfake.Hits = new Random().Next( 34, 10039343 ); topicfake.ReplyCount = 3; db.insert( topicfake ); Assert.Greater( topicfake.Id, 0 ); // 直接添加父类的具体数据 TCategory category = new TCategory(); category.Name = "post帖子分类"; db.insert( category ); TDataRoot root = new TDataRoot(); root.Title = "zzParent我是父类之init初始化"; root.Body = "父类的内容之init初始化"; root.Category = category; db.insert( root ); } insertAbstractTest(); }
public JsonResult postAdd(TPost post) { db.Add(post); db.SaveChanges(); return(Json(post)); }
public IActionResult Post_Create(CPostViewModel cPost, string[] tag) { try { for (int i = 0; i < tag.Count(); i++) { Tag.Add(tag[i]); } user = cPost.FUserId; //context.Add(cPost); TPost new_Post = new TPost { FUserId = cPost.FUserId, //todo FDescription = cPost.FDescription, FPeople = cPost.FPeople, FPostCity = cPost.FPostCity, FPostAddress = cPost.FPostAddress, FPostDistrict = cPost.FPostDistrict, FPostTime = cPost.FPostTime, FSportName = cPost.FSportName, FSystemTime = DateTime.Now.ToString(), FTitle = cPost.FTitle, }; context.TPosts.Add(new_Post); context.SaveChanges(); foreach (var i in Tag) //標籤 { TPostTag postTag = new TPostTag(); postTag.FTagId = int.Parse(i); postTag.FPostId = new_Post.FPostId; context.TPostTags.Add(postTag); context.SaveChanges(); } TJoinPerson joinPerson = new TJoinPerson(); //參加人 joinPerson.FUserId = new_Post.FUserId; joinPerson.FPostId = new_Post.FPostId; joinPerson.FJoinTime = DateTime.Now.ToString(); TPostSport postSport = new TPostSport(); //活動種類 postSport.FPostId = new_Post.FPostId; postSport.FSportName = new_Post.FSportName; context.TJoinPeople.Add(joinPerson); context.SaveChanges(); if (cPost.image != null) { foreach (var i in cPost.image) { string photoName = Guid.NewGuid().ToString() + ".jpg"; using (var photo = new FileStream(iv_host.ContentRootPath + @"/wwwroot/Postimg/" + photoName, FileMode.Create)) { i.CopyTo(photo); } TPostPhoto postPhoto = new TPostPhoto(); postPhoto.FPostId = new_Post.FPostId; postPhoto.FPostPhoto = @"/Postimg/" + photoName; context.TPostPhotos.Add(postPhoto); context.SaveChanges(); } } return(RedirectToAction("Post_Index", "CPost")); } catch { return(RedirectToAction("Post_Index", "CPost")); } }
public IActionResult Delete(int?FPostId) { try { if (FPostId != null) { var postMsged = context.TPostMsgeds.Where(n => n.FPostId == FPostId); if (postMsged != null) { foreach (var msged in postMsged) { context.TPostMsgeds.Remove(msged); } } var postCategory = context.TPostTags.Where(n => n.FPostId == FPostId); if (postCategory != null) { foreach (var category in postCategory) { context.TPostTags.Remove(category); } } var postMsg = context.TPostMsgs.Where(n => n.FPostId == FPostId); if (postMsg != null) { foreach (var msg in postMsg) { context.TPostMsgs.Remove(msg); } } var postLikeCount = context.TPostLikeCounts.Where(n => n.FPostId == FPostId); if (postLikeCount != null) { foreach (var like in postLikeCount) { context.TPostLikeCounts.Remove(like); } } var postJoin = context.TJoinPeople.Where(n => n.FPostId == FPostId); if (postJoin != null) { foreach (var join in postJoin) { context.TJoinPeople.Remove(join); } } var postPhoto = context.TPostPhotos.Where(n => n.FPostId == FPostId); if (postPhoto != null) { foreach (var photo in postPhoto) { context.TPostPhotos.Remove(photo); } } var postTag = context.TPostTags.Where(n => n.FPostId == FPostId); if (postTag != null) { foreach (var tag in postTag) { context.TPostTags.Remove(tag); } } TPost post = context.TPosts.FirstOrDefault(n => n.FPostId == FPostId); if (post != null) { context.TPosts.Remove(post); } context.SaveChanges(); } return(RedirectToAction("Post_Index", "CPost")); } catch { return(RedirectToAction("Post_Index", "CPost")); } }