Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1. using EF_tutorial;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.ComponentModel.DataAnnotations;
  8. using System.Data.Entity;
  9. using Newtonsoft.Json;
  10. using System.IO;
  11.  
  12. namespace EF_tutorial
  13. {
  14. class Program
  15. {
  16. static void Main(string[] args)
  17. {
  18. USER sampleUser1 = new USER
  19. {
  20. Username = "Jakinator",
  21. Password = "IllBeBack"
  22. };
  23.  
  24. USER sampleUser2 = new USER
  25. {
  26. Username = "ILoveKittens",
  27. Password = "meow"
  28. };
  29.  
  30. USER sampleUser3 = new USER
  31. {
  32. Username = "SoccerMom",
  33. Password = "MyKidsAnHonorStudent"
  34. };
  35.  
  36. BLOG_POSTS samplePost = new BLOG_POSTS
  37. {
  38. DatePosted = DateTime.Now,
  39. Title = "Why Terminator is the best movie ever!",
  40. Body = "Terminator is the best movie ever because it's the only one I've ever seen. It was so awesome that when I finished watching it I vowed to never watch another movie again."
  41. };
  42. using (var blogo = new MyEntities())
  43. {
  44. blogo.BLOG_POSTS.Add(samplePost);
  45. }
  46.  
  47. POST_COMMENTS sampleComment1 = new POST_COMMENTS
  48. {
  49. UserId = sampleUser1.Id,
  50. DatePosted = DateTime.Now,
  51. Body = "First! I'm always the first to comment on my own blog posts. I'm just awesome like that."
  52. };
  53.  
  54. POST_COMMENTS sampleComment2 = new POST_COMMENTS
  55. {
  56. UserId = sampleUser2.Id,
  57. DatePosted = DateTime.Now,
  58. Body = "Jake I completely disagree that Terminator is the best movie ever for one reason: There were no kittens in Terminator. If there were kittens, then maybe..."
  59. };
  60.  
  61. POST_COMMENTS sampleComment3 = new POST_COMMENTS
  62. {
  63. UserId = sampleUser3.Id,
  64. DatePosted = DateTime.Now,
  65. Body = "I would never let my kids watch horrible movies like this. They should be at soccer practice or doing homework. That's why my kids have all straight A's and are all honor students."
  66. };
  67.  
  68. using (var com = new MyEntities())
  69. {
  70. com.USERS.Add(sampleUser1);
  71. //com.USERS.Add(sampleUser2);
  72. //com.USERS.Add(sampleUser3);
  73. com.POST_COMMENTS.Add(sampleComment1);/*
  74. com.POST_COMMENTS.Add(sampleComment2);
  75. com.POST_COMMENTS.Add(sampleComment3);*/
  76. com.BLOG_POSTS.Add(samplePost);
  77.  
  78. com.SaveChanges();
  79. }
  80. using (var dej = new MyEntities())
  81. {
  82. var blogs = (from b in dej.USERS
  83. select b).ToList();
  84. Console.WriteLine("ALL POSTS:");
  85. foreach (var blog in blogs)
  86. Console.WriteLine(" " + blog.Username);
  87.  
  88.  
  89. }
  90. using (var dej = new MyEntities())
  91. {
  92. var bos = (from s in dej.POST_COMMENTS
  93. select s).ToList();
  94. foreach (var sd in bos)
  95. Console.WriteLine("" + sd.Body);
  96. }
  97. Console.WriteLine("asdasdasd");
  98. dbToJson1();
  99. Console.ReadLine();
  100. }
  101. public class MYUSERS
  102. {
  103. public string user { get; set; }
  104. public string pass { get; set; }
  105. }
  106. public class MYPOSTS
  107. {
  108. //public int id { get; set; }
  109. public int uid { get; set; }
  110. public System.DateTime date { get; set; }
  111. public string ptitle { get; set; }
  112. public string pbody { get; set; }
  113. }
  114.  
  115. public class MYCOMMENTS
  116. {
  117. //public int Id { get; set; }
  118. public int BlogPostId { get; set; }
  119. public int UserId { get; set; }
  120. public System.DateTime DatePosted { get; set; }
  121. public string Body { get; set; }
  122. }
  123. /*
  124. public static void dbToJson()
  125. {
  126. var dej = new MyEntities();
  127. IQueryable<MySystem> sysList = from s in dej.USERS
  128. select new MySystem()
  129. {
  130. user = s.Username,
  131. pass = s.Password
  132. };
  133. MySystem sys = sysList.First();
  134. string joson = JsonConvert.SerializeObject(sys);
  135. File.WriteAllText(@"json.json", joson.ToString());
  136. }*/
  137. public static void dbToJson1()
  138. {
  139. var dej = new MyEntities();
  140. IQueryable<MYCOMMENTS> sysList2 = from s in dej.POST_COMMENTS
  141. select new MYCOMMENTS()
  142. {
  143. //Id = s.Id,
  144. BlogPostId = s.BlogPostId,
  145. UserId = s.UserId,
  146. DatePosted = s.DatePosted,
  147. Body = s.Body
  148. };
  149. IQueryable<MYPOSTS> sysList1 = from s in dej.BLOG_POSTS
  150. select new MYPOSTS()
  151. {
  152. //id = s.Id,
  153. uid = s.UserID,
  154. date = s.DatePosted,
  155. ptitle = s.Title,
  156. pbody = s.Body
  157. };
  158. IQueryable<MYUSERS> sysList = from s in dej.USERS
  159. select new MYUSERS()
  160. {
  161. user = s.Username,
  162. pass = s.Password
  163. };
  164. List<MYCOMMENTS> com = new List<MYCOMMENTS>(sysList2);
  165. List<MYPOSTS> pos = new List<MYPOSTS>(sysList1);
  166. List<MYUSERS> use = new List<MYUSERS>(sysList);
  167. string juse = JsonConvert.SerializeObject(use);
  168. string jpos = JsonConvert.SerializeObject(pos);
  169. string jcom = JsonConvert.SerializeObject(com);
  170. File.WriteAllText(@"json.json", juse.ToString());
  171. File.AppendAllText(@"json.json",jpos.ToString() + Environment.NewLine);
  172. File.AppendAllText(@"json.json", jcom.ToString() + Environment.NewLine);
  173. }
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement