Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BloggingContext : DbContext
- {
- public BloggingContext(DbContextOptions<BloggingContext> options) : base(options){ }
- public virtual DbSet<Blog> Blogs { get; set; }
- public virtual DbSet<Post> Posts { get; set; }
- }
- public class Blog
- {
- public int BlogId { get; set; }
- public string Url { get; set; }
- public ICollection<Post> Posts { get; set; }
- }
- public class Post
- {
- public int PostId { get; set; }
- public string Title { get; set; }
- public string Content { get; set; }
- public int BlogId { get; set; }
- public Blog Blog { get; set; }
- }
- private readonly BloggingContext db;
- public ValuesController(BloggingContext db)
- {
- this.db = db;
- }
- // GET api/values
- [HttpGet]
- public ActionResult<IEnumerable<Blog>> Get()
- {
- return db.Blogs.ToList();
- }
- var connection = @"Server=(localdb)mssqllocaldb;Database=EFGetStarted.AspNetCore.NewDb;Trusted_Connection=True;ConnectRetryCount=0";
- services.AddDbContext<Models.Model.BloggingContext>
- (options => options.UseSqlServer(connection));
- Blogs Table Result
- __________________
- BlogId | Url
- 1 | asdasd1
- 2 | asdas2
- Posts Table Result
- __________
- PostId | Title | Content | BlogId
- 1 | asdasd | fdg | 1
- 2 | fsg | asda | 1
- 3 | dsgfsdg | sgf | 2
- [
- {
- "blogId": 1,
- "url": "asdasd1",
- "posts": null
- },
- {
- "blogId": 2,
- "url": "asdas2",
- "posts": null
- },
- {
- "blogId": 3,
- "url": "asdsad3",
- "posts": null
- }
- ]
- [
- {
- "blogId": 1,
- "url": "asdasd1",
- "posts": [
- {
- "PostId": 1,
- "Title": "asdasd",
- "Content": "fdg"
- },
- {
- "PostId": 2,
- "Title": "fsg",
- "Content": "asda"
- }
- ]
- },
- {
- "blogId": 2,
- "url": "asdas2",
- "posts": {
- "PostId": 2,
- "Title": "fsg",
- "Content": "asda"
- }
- },
- {
- "blogId": 3,
- "url": "asdsad3",
- "posts": null
- }
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement