Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. public class BloggingContext : DbContext
  2. {
  3. public BloggingContext(DbContextOptions<BloggingContext> options) : base(options){ }
  4.  
  5. public virtual DbSet<Blog> Blogs { get; set; }
  6. public virtual DbSet<Post> Posts { get; set; }
  7. }
  8.  
  9. public class Blog
  10. {
  11. public int BlogId { get; set; }
  12. public string Url { get; set; }
  13.  
  14. public ICollection<Post> Posts { get; set; }
  15. }
  16.  
  17. public class Post
  18. {
  19. public int PostId { get; set; }
  20. public string Title { get; set; }
  21. public string Content { get; set; }
  22.  
  23. public int BlogId { get; set; }
  24. public Blog Blog { get; set; }
  25. }
  26.  
  27. private readonly BloggingContext db;
  28.  
  29. public ValuesController(BloggingContext db)
  30. {
  31. this.db = db;
  32. }
  33.  
  34. // GET api/values
  35. [HttpGet]
  36. public ActionResult<IEnumerable<Blog>> Get()
  37. {
  38. return db.Blogs.ToList();
  39. }
  40.  
  41. var connection = @"Server=(localdb)mssqllocaldb;Database=EFGetStarted.AspNetCore.NewDb;Trusted_Connection=True;ConnectRetryCount=0";
  42. services.AddDbContext<Models.Model.BloggingContext>
  43. (options => options.UseSqlServer(connection));
  44.  
  45. Blogs Table Result
  46. __________________
  47. BlogId | Url
  48. 1 | asdasd1
  49. 2 | asdas2
  50.  
  51. Posts Table Result
  52. __________
  53. PostId | Title | Content | BlogId
  54. 1 | asdasd | fdg | 1
  55. 2 | fsg | asda | 1
  56. 3 | dsgfsdg | sgf | 2
  57.  
  58. [
  59. {
  60. "blogId": 1,
  61. "url": "asdasd1",
  62. "posts": null
  63. },
  64. {
  65. "blogId": 2,
  66. "url": "asdas2",
  67. "posts": null
  68. },
  69. {
  70. "blogId": 3,
  71. "url": "asdsad3",
  72. "posts": null
  73. }
  74. ]
  75.  
  76. [
  77. {
  78. "blogId": 1,
  79. "url": "asdasd1",
  80. "posts": [
  81. {
  82. "PostId": 1,
  83. "Title": "asdasd",
  84. "Content": "fdg"
  85. },
  86. {
  87. "PostId": 2,
  88. "Title": "fsg",
  89. "Content": "asda"
  90. }
  91. ]
  92. },
  93. {
  94. "blogId": 2,
  95. "url": "asdas2",
  96. "posts": {
  97. "PostId": 2,
  98. "Title": "fsg",
  99. "Content": "asda"
  100. }
  101. },
  102. {
  103. "blogId": 3,
  104. "url": "asdsad3",
  105. "posts": null
  106. }
  107. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement