Skip to content

Commit

Permalink
Add sample groupby issue query in ef 3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
phucthaidoan committed Nov 3, 2023
1 parent 250df9a commit 124467e
Show file tree
Hide file tree
Showing 10 changed files with 562 additions and 4 deletions.
11 changes: 9 additions & 2 deletions EFCoreSamples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ VisualStudioVersion = 17.7.34202.233
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Querying", "Querying", "{44F1D95E-F174-4676-8C14-D02B5E0DA764}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QueryingIssues.EF7", "QueryingIssues\QueryingIssues.EF7.csproj", "{3B3EF0D2-E8AC-453E-BA4D-9BEC27C000B1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QueryingIssues.EF7", "QueryingIssues\QueryingIssues.EF7.csproj", "{3B3EF0D2-E8AC-453E-BA4D-9BEC27C000B1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Logging", "Logging\Logging.csproj", "{2A4A63A3-BFC8-4D45-83AD-7F34CF694418}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Logging", "Logging\Logging.csproj", "{2A4A63A3-BFC8-4D45-83AD-7F34CF694418}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QueryingIssues.EF3.1", "QueryingIssues.EF3.1\QueryingIssues.EF3.1.csproj", "{3A87659F-A062-463B-BD4C-55698E97218C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -23,13 +25,18 @@ Global
{2A4A63A3-BFC8-4D45-83AD-7F34CF694418}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2A4A63A3-BFC8-4D45-83AD-7F34CF694418}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2A4A63A3-BFC8-4D45-83AD-7F34CF694418}.Release|Any CPU.Build.0 = Release|Any CPU
{3A87659F-A062-463B-BD4C-55698E97218C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3A87659F-A062-463B-BD4C-55698E97218C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3A87659F-A062-463B-BD4C-55698E97218C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3A87659F-A062-463B-BD4C-55698E97218C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{3B3EF0D2-E8AC-453E-BA4D-9BEC27C000B1} = {44F1D95E-F174-4676-8C14-D02B5E0DA764}
{2A4A63A3-BFC8-4D45-83AD-7F34CF694418} = {44F1D95E-F174-4676-8C14-D02B5E0DA764}
{3A87659F-A062-463B-BD4C-55698E97218C} = {44F1D95E-F174-4676-8C14-D02B5E0DA764}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {512678AA-D8DE-42B9-9E6F-45A28E1EC6CD}
Expand Down
73 changes: 73 additions & 0 deletions QueryingIssues.EF3.1/Migrations/20231103163846_Init.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions QueryingIssues.EF3.1/Migrations/20231103163846_Init.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

namespace QueryingIssues.EF3._1.Migrations
{
public partial class Init : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Blogs",
columns: table => new
{
BlogId = table.Column<int>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Url = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Blogs", x => x.BlogId);
});

migrationBuilder.CreateTable(
name: "Posts",
columns: table => new
{
PostId = table.Column<int>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Title = table.Column<string>(nullable: true),
Content = table.Column<string>(nullable: true),
BlogId = table.Column<int>(nullable: false),
CreatedAt = table.Column<DateTime>(nullable: false),
NumberOfView = table.Column<long>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Posts", x => x.PostId);
table.ForeignKey(
name: "FK_Posts_Blogs_BlogId",
column: x => x.BlogId,
principalTable: "Blogs",
principalColumn: "BlogId",
onDelete: ReferentialAction.Cascade);
});

migrationBuilder.CreateIndex(
name: "IX_Posts_BlogId",
table: "Posts",
column: "BlogId");
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Posts");

migrationBuilder.DropTable(
name: "Blogs");
}
}
}
71 changes: 71 additions & 0 deletions QueryingIssues.EF3.1/Migrations/BloggingContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

namespace QueryingIssues.EF3._1.Migrations
{
[DbContext(typeof(BloggingContext))]
partial class BloggingContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "3.1.32");

modelBuilder.Entity("Blog", b =>
{
b.Property<int>("BlogId")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");

b.Property<string>("Url")
.HasColumnType("TEXT");

b.HasKey("BlogId");

b.ToTable("Blogs");
});

modelBuilder.Entity("Post", b =>
{
b.Property<int>("PostId")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");

b.Property<int>("BlogId")
.HasColumnType("INTEGER");

b.Property<string>("Content")
.HasColumnType("TEXT");

b.Property<DateTime>("CreatedAt")
.HasColumnType("TEXT");

b.Property<long>("NumberOfView")
.HasColumnType("INTEGER");

b.Property<string>("Title")
.HasColumnType("TEXT");

b.HasKey("PostId");

b.HasIndex("BlogId");

b.ToTable("Posts");
});

modelBuilder.Entity("Post", b =>
{
b.HasOne("Blog", "Blog")
.WithMany("Posts")
.HasForeignKey("BlogId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
#pragma warning restore 612, 618
}
}
}
52 changes: 52 additions & 0 deletions QueryingIssues.EF3.1/Model.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;

public class BloggingContext : DbContext
{
public static readonly ILoggerFactory MyLoggerFactory
= LoggerFactory.Create(builder => builder.AddConsole());

public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }

public string DbPath { get; }

public BloggingContext()
{
var folder = Environment.SpecialFolder.LocalApplicationData;
var path = Environment.GetFolderPath(folder);
DbPath = System.IO.Path.Join(path, "blogging3.1.db");
}

// The following configures EF to create a Sqlite database file in the
// special "local" folder for your platform.
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options
.UseLoggerFactory(MyLoggerFactory) // Enable EF Core logging
// .UseSqlServer(
// "Server=LAPTOP-IAJ1J0A2;Database=blogpost;Trusted_Connection=True;MultipleActiveResultSets=true;TrustServerCertificate=true");
.UseSqlite($"Data Source={DbPath}");
}

public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }

public List<Post> Posts { get; } = new List<Post>();
}

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; }
public DateTime CreatedAt { get; set; }

public long NumberOfView { get; set; }
}
14 changes: 14 additions & 0 deletions QueryingIssues.EF3.1/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace QueryingIssues.EF3._1
{
public class Program
{
static void Main(string[] args)
{
var query5 = new QueryStackoverflowGroupBy();
query5.QueryNo59456026();
query5.QueryNo59456026_Workaround();
}
}
}
Loading

0 comments on commit 124467e

Please sign in to comment.