From 8de4b22fbc5be56ed99e72c275c5e0926ad5d73a Mon Sep 17 00:00:00 2001 From: Tran Minh Luan Date: Wed, 31 Jul 2024 17:58:31 +0700 Subject: [PATCH] src/demo: add types, foreign keys ... (#9) --- .github/workflows/build.yaml | 2 +- .../Atlas.Provider.Demo.csproj | 3 + src/Atlas.Provider.Demo/Program.cs | 77 +++++++++++++++++-- 3 files changed, 76 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index ab6426a..afb8b3f 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -54,7 +54,7 @@ jobs: - name: Print SQL from Demo project shell: pwsh working-directory: ./src/Atlas.Provider.Demo - run: dotnet atlas-ef + run: dotnet atlas-ef -- sqlserver - name: Publish artifacts uses: actions/upload-artifact@v3.1.3 with: diff --git a/src/Atlas.Provider.Demo/Atlas.Provider.Demo.csproj b/src/Atlas.Provider.Demo/Atlas.Provider.Demo.csproj index 92fc873..6ab4ad6 100644 --- a/src/Atlas.Provider.Demo/Atlas.Provider.Demo.csproj +++ b/src/Atlas.Provider.Demo/Atlas.Provider.Demo.csproj @@ -13,5 +13,8 @@ all + + + \ No newline at end of file diff --git a/src/Atlas.Provider.Demo/Program.cs b/src/Atlas.Provider.Demo/Program.cs index 9db3e96..bcc835a 100644 --- a/src/Atlas.Provider.Demo/Program.cs +++ b/src/Atlas.Provider.Demo/Program.cs @@ -1,6 +1,8 @@ using Microsoft.EntityFrameworkCore; -using System.Reflection.Metadata; -using Microsoft.EntityFrameworkCore.SqlServer; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using Microsoft.EntityFrameworkCore.Design; +using Pomelo.EntityFrameworkCore.MySql.Infrastructure; namespace DemoNamespace { @@ -8,27 +10,92 @@ public class Program { public static void Main(string[] args) { - } } + public class BloggingContextFactory : IDesignTimeDbContextFactory + { + public BloggingContext CreateDbContext(string[] args) + { + var provider = args.FirstOrDefault(); + + return new BloggingContext(provider); + } + } public class BloggingContext : DbContext { public DbSet Blogs { get; set; } + private readonly string _provider; + public BloggingContext(string provider = "SqlServer") + { + _provider = provider; + } protected override void OnConfiguring(DbContextOptionsBuilder options) { - options.UseSqlServer("foo"); + switch (_provider.ToLower()) + { + case "sqlserver": + options.UseSqlServer("Server=localhost;Database=YourDatabaseName;User Id=your_username;Password=your_password;"); + break; + case "sqlite": + options.UseSqlite("Data Source=localdatabase.db;"); + break; + case "mysql": + options.UseMySql("Server=localhost;Database=YourDatabaseName;User=root;Password=your_password;", ServerVersion.Create(8, 0, 0, ServerType.MySql)); + break; + case "mariadb": + options.UseMySql("Server=localhost;Database=YourDatabaseName;User=root;Password=your_password;", ServerVersion.Create(8, 7, 0, ServerType.MariaDb)); + break; + case "postgres": + options.UseNpgsql("Host=localhost;Database=YourDatabaseName;Username=your_username;Password=your_password;"); + break; + } + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity() + .HasOne(p => p.Blog) + .WithMany(b => b.Posts) + .HasForeignKey(p => p.BlogUrl) + .HasPrincipalKey(b => b.Url); + + modelBuilder.Entity() + .HasMany(b => b.Posts) + .WithOne(p => p.Blog) + .HasForeignKey(p => p.BlogUrl); + + modelBuilder.Entity() + .Property(b => b.Author) + .HasDefaultValue("Anonymous") + .HasMaxLength(200); } } public class Blog { + [Key] public int BlogId { get; set; } - public string Url { get; set; } = string.Empty; + + [Column(TypeName = "varchar(200)")] + public string Url { get; set; } + + [Column(TypeName = "decimal(5, 2)")] + public decimal Rating { get; set; } public string Title { get; set; } = string.Empty; public string Content { get; set; } = string.Empty; public string Author { get; set; } = string.Empty; + public List Posts { get; set; } + } + + public class Post + { + [Key] public int PostId { get; set; } + public string Title { get; set; } = string.Empty; + public string Content { get; set; } = string.Empty; + public string BlogUrl { get; set; } + public Blog Blog { get; set; } } } \ No newline at end of file