Skip to content

Commit 1fa6d43

Browse files
authored
Merge pull request #12 from ewdlop/ewdlop-patch-10
Add DataAccessLayer console project and initial migration for Blogs a…
2 parents f740bb3 + 52d11de commit 1fa6d43

11 files changed

Lines changed: 317 additions & 38 deletions

File tree

亂七八糟/亂七八糟.Console/亂七八糟.Console.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
</ItemGroup>
1818

1919
<ItemGroup>
20-
<ProjectReference Include="..\亂七八糟.SourceGenerators\亂七八糟.SourceGenerators.csproj"
21-
OutputItemType="Analyzer"
22-
ReferenceOutputAssembly="false" />
20+
<ProjectReference Include="..\亂七八糟.SourceGenerators\亂七八糟.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
2321
</ItemGroup>
2422

2523
</Project>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// See https://aka.ms/new-console-template for more information
2+
Console.WriteLine("Hello, World!");
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.2">
12+
<PrivateAssets>all</PrivateAssets>
13+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
14+
</PackageReference>
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\亂七八糟.DataAccessLayer\亂七八糟.DataAccessLayer.csproj" />
19+
</ItemGroup>
20+
21+
</Project>

亂七八糟/亂七八糟.DataAccessLayer/BloggingContext.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ namespace 亂七八糟.DataAccessLayer;
77
/// </summary>
88
public class BloggingContext : DbContext
99
{
10-
public static readonly BloggingContext Default = new BloggingContext();
11-
public DbSet<Blog>? Blogs { get; set; } = Default.Blogs;
12-
public DbSet<Post>? Posts { get; set; } = Default.Posts;
10+
//public static readonly BloggingContext Default = new BloggingContext();
11+
//public required DbSet<Blog> Blogs { get; set; }
12+
//public required DbSet<Post>? Posts { get; set; }
13+
public DbSet<Blog>? Blogs { get; set; }
14+
public DbSet<Post>? Posts { get; set; }
1315
public string? DbPath { get; } = string.Empty;
1416

1517
public BloggingContext()
@@ -19,7 +21,7 @@ public BloggingContext()
1921
DbPath = System.IO.Path.Join(path, "blogging.db");
2022
}
2123

22-
public BloggingContext(DbContextOptions options) : base(options)
24+
public BloggingContext(DbContextOptions<BloggingContext> options) : base(options)
2325
{
2426
Environment.SpecialFolder folder = Environment.SpecialFolder.LocalApplicationData;
2527
string? path = Environment.GetFolderPath(folder);
@@ -33,8 +35,23 @@ public BloggingContext(BloggingContext context)
3335
DbPath = context.DbPath;
3436
}
3537

38+
public BloggingContext(BloggingContext context, string dbPath)
39+
{
40+
Blogs = context.Set<Blog>();
41+
Posts = context.Set<Post>();
42+
DbPath = dbPath;
43+
}
44+
45+
//public BloggingContext(string dbPath)
46+
//{
47+
// DbPath = dbPath;
48+
//}
49+
3650
// The following configures EF to create a Sqlite database file in the
3751
// special "local" folder for your platform.
3852
protected override void OnConfiguring(DbContextOptionsBuilder options)
39-
=> options.UseSqlite($"Data Source={DbPath}");
40-
}
53+
{
54+
if(!options.IsConfigured)
55+
options.UseSqlite($"Data Source={DbPath}");
56+
}
57+
}

亂七八糟/亂七八糟.DataAccessLayer/Migrations/20250305211823_AddBlogAndPost.Designer.cs

Lines changed: 79 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using Microsoft.EntityFrameworkCore.Migrations;
2+
3+
#nullable disable
4+
5+
namespace 亂七八糟.DataAccessLayer.Migrations
6+
{
7+
/// <inheritdoc />
8+
public partial class AddBlogAndPost : Migration
9+
{
10+
/// <inheritdoc />
11+
protected override void Up(MigrationBuilder migrationBuilder)
12+
{
13+
migrationBuilder.CreateTable(
14+
name: "Blogs",
15+
columns: table => new
16+
{
17+
BlogId = table.Column<int>(type: "INTEGER", nullable: false)
18+
.Annotation("Sqlite:Autoincrement", true),
19+
Url = table.Column<string>(type: "TEXT", nullable: false)
20+
},
21+
constraints: table =>
22+
{
23+
table.PrimaryKey("PK_Blogs", x => x.BlogId);
24+
});
25+
26+
migrationBuilder.CreateTable(
27+
name: "Posts",
28+
columns: table => new
29+
{
30+
PostId = table.Column<int>(type: "INTEGER", nullable: false)
31+
.Annotation("Sqlite:Autoincrement", true),
32+
Title = table.Column<string>(type: "TEXT", nullable: false),
33+
Content = table.Column<string>(type: "TEXT", nullable: false),
34+
BlogId = table.Column<int>(type: "INTEGER", nullable: false)
35+
},
36+
constraints: table =>
37+
{
38+
table.PrimaryKey("PK_Posts", x => x.PostId);
39+
table.ForeignKey(
40+
name: "FK_Posts_Blogs_BlogId",
41+
column: x => x.BlogId,
42+
principalTable: "Blogs",
43+
principalColumn: "BlogId",
44+
onDelete: ReferentialAction.Cascade);
45+
});
46+
47+
migrationBuilder.CreateIndex(
48+
name: "IX_Posts_BlogId",
49+
table: "Posts",
50+
column: "BlogId");
51+
}
52+
53+
/// <inheritdoc />
54+
protected override void Down(MigrationBuilder migrationBuilder)
55+
{
56+
migrationBuilder.DropTable(
57+
name: "Posts");
58+
59+
migrationBuilder.DropTable(
60+
name: "Blogs");
61+
}
62+
}
63+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// <auto-generated />
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.EntityFrameworkCore.Infrastructure;
4+
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
5+
using 亂七八糟.DataAccessLayer;
6+
7+
#nullable disable
8+
9+
namespace 亂七八糟.DataAccessLayer.Migrations
10+
{
11+
[DbContext(typeof(BloggingContext))]
12+
partial class BloggingContextModelSnapshot : ModelSnapshot
13+
{
14+
protected override void BuildModel(ModelBuilder modelBuilder)
15+
{
16+
#pragma warning disable 612, 618
17+
modelBuilder.HasAnnotation("ProductVersion", "9.0.2");
18+
19+
modelBuilder.Entity("亂七八糟.DataAccessLayer.Blog", b =>
20+
{
21+
b.Property<int>("BlogId")
22+
.ValueGeneratedOnAdd()
23+
.HasColumnType("INTEGER");
24+
25+
b.Property<string>("Url")
26+
.IsRequired()
27+
.HasColumnType("TEXT");
28+
29+
b.HasKey("BlogId");
30+
31+
b.ToTable("Blogs");
32+
});
33+
34+
modelBuilder.Entity("亂七八糟.DataAccessLayer.Post", b =>
35+
{
36+
b.Property<int>("PostId")
37+
.ValueGeneratedOnAdd()
38+
.HasColumnType("INTEGER");
39+
40+
b.Property<int>("BlogId")
41+
.HasColumnType("INTEGER");
42+
43+
b.Property<string>("Content")
44+
.IsRequired()
45+
.HasColumnType("TEXT");
46+
47+
b.Property<string>("Title")
48+
.IsRequired()
49+
.HasColumnType("TEXT");
50+
51+
b.HasKey("PostId");
52+
53+
b.HasIndex("BlogId");
54+
55+
b.ToTable("Posts");
56+
});
57+
58+
modelBuilder.Entity("亂七八糟.DataAccessLayer.Post", b =>
59+
{
60+
b.HasOne("亂七八糟.DataAccessLayer.Blog", "Blog")
61+
.WithMany("Posts")
62+
.HasForeignKey("BlogId")
63+
.OnDelete(DeleteBehavior.Cascade)
64+
.IsRequired();
65+
66+
b.Navigation("Blog");
67+
});
68+
69+
modelBuilder.Entity("亂七八糟.DataAccessLayer.Blog", b =>
70+
{
71+
b.Navigation("Posts");
72+
});
73+
#pragma warning restore 612, 618
74+
}
75+
}
76+
}
Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System;
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Hosting;
4+
using System;
25
using System.Linq;
36
using 亂七八糟.DataAccessLayer;
47

@@ -12,33 +15,43 @@ public class Program
1215
/// <param name="args"></param>
1316
public static async Task Main(string[] args)
1417
{
15-
await using var db = new BloggingContext();
16-
17-
// Note: This sample requires the database to be created before running.
18-
Console.WriteLine($"Database path: {db.DbPath}.");
19-
20-
// Create
21-
Console.WriteLine("Inserting a new blog");
22-
db.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
23-
db.SaveChanges();
24-
25-
// Read
26-
Console.WriteLine("Querying for a blog");
27-
var blog = db.Blogs
28-
.OrderBy(b => b.BlogId)
29-
.First();
30-
31-
// Update
32-
Console.WriteLine("Updating the blog and adding a post");
33-
blog.Url = "https://devblogs.microsoft.com/dotnet";
34-
blog.Posts.Add(
35-
new Post { Title = "Hello World", Content = "I wrote an app using EF Core!" });
36-
db.SaveChanges();
37-
38-
// Delete
39-
Console.WriteLine("Delete the blog");
40-
db.Remove(blog);
41-
db.SaveChanges();
42-
18+
var host = Host.CreateDefaultBuilder(args)
19+
.ConfigureServices(services =>
20+
{
21+
services.AddDbContext<BloggingContext>(options =>
22+
options.UseSqlite("Data Source=blogging.db"));
23+
}).Build();
24+
25+
using (var scope = host.Services.CreateScope())
26+
{
27+
await using var db = new BloggingContext();
28+
db.Database.EnsureCreated();
29+
30+
// Note: This sample requires the database to be created before running.
31+
Console.WriteLine($"Database path: {db.DbPath}.");
32+
33+
// Create
34+
Console.WriteLine("Inserting a new blog");
35+
db.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
36+
db.SaveChanges();
37+
38+
// Read
39+
Console.WriteLine("Querying for a blog");
40+
var blog = db.Blogs
41+
.OrderBy(b => b.BlogId)
42+
.First();
43+
44+
// Update
45+
Console.WriteLine("Updating the blog and adding a post");
46+
blog.Url = "https://devblogs.microsoft.com/dotnet";
47+
blog.Posts.Add(
48+
new Post { Title = "Hello World", Content = "I wrote an app using EF Core!" });
49+
db.SaveChanges();
50+
51+
// Delete
52+
Console.WriteLine("Delete the blog");
53+
db.Remove(blog);
54+
db.SaveChanges();
55+
}
4356
}
4457
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# README
2+
3+
## dacpac

0 commit comments

Comments
 (0)