// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.Linq; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; #pragma warning disable 169 namespace Samples { public class Program { private static void Main() { SetupDatabase(); using (var db = new BloggingContext()) { #region Query var postCounts = db.BlogPostCounts.ToList(); foreach (var postCount in postCounts) { Console.WriteLine($"{postCount.BlogName} has {postCount.PostCount} posts."); Console.WriteLine(); } #endregion } } private static void SetupDatabase() { using (var db = new BloggingContext()) { if (db.Database.EnsureCreated()) { db.Blogs.Add( new Blog { Name = "Fish Blog", Url = "http://sample.com/blogs/fish", Posts = new List { new Post { Title = "Fish care 101" }, new Post { Title = "Caring for tropical fish" }, new Post { Title = "Types of ornamental fish" } } }); db.Blogs.Add( new Blog { Name = "Cats Blog", Url = "http://sample.com/blogs/cats", Posts = new List { new Post { Title = "Cat care 101" }, new Post { Title = "Caring for tropical cats" }, new Post { Title = "Types of ornamental cats" } } }); db.Blogs.Add( new Blog { Name = "Catfish Blog", Url = "http://sample.com/blogs/catfish", Posts = new List { new Post { Title = "Catfish care 101" }, new Post { Title = "History of the catfish name" } } }); db.SaveChanges(); #region View db.Database.ExecuteSqlCommand( @"CREATE VIEW View_BlogPostCounts AS SELECT Name, Count(p.PostId) as PostCount from Blogs b JOIN Posts p on p.BlogId = b.BlogId GROUP BY b.Name"); #endregion } } } } public class BloggingContext : DbContext { private static readonly ILoggerFactory _loggerFactory = new LoggerFactory().AddConsole((s, l) => l == LogLevel.Information && !s.EndsWith("Connection")); public DbSet Blogs { get; set; } public DbSet Posts { get; set; } public DbQuery BlogPostCounts { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder .UseSqlServer( @"Server=(localdb)\mssqllocaldb;Database=Sample.QueryTypes;Trusted_Connection=True;ConnectRetryCount=0;") .UseLoggerFactory(_loggerFactory); } #region Configuration protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder .Query().ToTable("View_BlogPostCounts") .Property(v => v.BlogName).HasColumnName("Name"); } #endregion } #region Entities public class Blog { public int BlogId { get; set; } public string Name { get; set; } public string Url { get; set; } public ICollection 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; } } #endregion #region QueryType public class BlogPostsCount { public string BlogName { get; set; } public int PostCount { get; set; } } #endregion }