Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions d2lang-cs.slnx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Solution>
<Folder Name="/example/">
<Project Path="example/cli/d2-sample-cli.csproj" />
<Project Path="example/entity-framework-core/d2-sample-ef-core.csproj" />
</Folder>
<Project Path="src/d2lang-cs.csproj" />
<Project Path="src/D2Lang.EntityFrameworkCore/D2Lang.EntityFrameworkCore.csproj" />
Expand Down
39 changes: 39 additions & 0 deletions example/entity-framework-core/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var builder = Host.CreateApplicationBuilder(args);

builder.Services
.AddDbContext<BlogDbContext>(options => options.UseSqlite("Data Source=blog.db"))
.AddD2Schema<BlogDbContext>("database-schema.d2");

using var host = builder.Build();
await host.StartAsync();
await host.StopAsync();

internal sealed class BlogDbContext : DbContext
{
public BlogDbContext(DbContextOptions<BlogDbContext> options)
: base(options)
{
}

public DbSet<Blog> Blogs => Set<Blog>();
public DbSet<Post> Posts => Set<Post>();
}

internal sealed class Blog
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public List<Post> Posts { get; set; } = new();
}

internal sealed class Post
{
public int Id { get; set; }
public string Title { get; set; } = string.Empty;
public int BlogId { get; set; }
public Blog Blog { get; set; } = null!;
}
21 changes: 21 additions & 0 deletions example/entity-framework-core/d2-sample-ef-core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<RootNamespace>d2_sample_ef_core</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.10" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.10" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\D2Lang.EntityFrameworkCore\D2Lang.EntityFrameworkCore.csproj" />
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions example/entity-framework-core/database-schema.d2
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Blogs: {
Id: INTEGER NOT NULL { constraint: primary_key }
Name: TEXT NOT NULL
shape: sql_table
}
Posts: {
Id: INTEGER NOT NULL { constraint: primary_key }
BlogId: INTEGER NOT NULL { constraint: foreign_key }
Title: TEXT NOT NULL
shape: sql_table
}
Posts.BlogId -> Blogs.Id