diff --git a/d2lang-cs.slnx b/d2lang-cs.slnx index a941ac3..e2ddf86 100644 --- a/d2lang-cs.slnx +++ b/d2lang-cs.slnx @@ -1,6 +1,7 @@ + diff --git a/example/entity-framework-core/Program.cs b/example/entity-framework-core/Program.cs new file mode 100644 index 0000000..fb55ac0 --- /dev/null +++ b/example/entity-framework-core/Program.cs @@ -0,0 +1,39 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +var builder = Host.CreateApplicationBuilder(args); + +builder.Services + .AddDbContext(options => options.UseSqlite("Data Source=blog.db")) + .AddD2Schema("database-schema.d2"); + +using var host = builder.Build(); +await host.StartAsync(); +await host.StopAsync(); + +internal sealed class BlogDbContext : DbContext +{ + public BlogDbContext(DbContextOptions options) + : base(options) + { + } + + public DbSet Blogs => Set(); + public DbSet Posts => Set(); +} + +internal sealed class Blog +{ + public int Id { get; set; } + public string Name { get; set; } = string.Empty; + public List 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!; +} diff --git a/example/entity-framework-core/d2-sample-ef-core.csproj b/example/entity-framework-core/d2-sample-ef-core.csproj new file mode 100644 index 0000000..e7822d5 --- /dev/null +++ b/example/entity-framework-core/d2-sample-ef-core.csproj @@ -0,0 +1,21 @@ + + + + Exe + net10.0 + d2_sample_ef_core + enable + enable + false + + + + + + + + + + + + diff --git a/example/entity-framework-core/database-schema.d2 b/example/entity-framework-core/database-schema.d2 new file mode 100644 index 0000000..2780677 --- /dev/null +++ b/example/entity-framework-core/database-schema.d2 @@ -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