Skip to content

Commit

Permalink
src/demo: add types, foreign keys ... (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
luantranminh authored Jul 31, 2024
1 parent 3367e43 commit 8de4b22
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
with:
Expand Down
3 changes: 3 additions & 0 deletions src/Atlas.Provider.Demo/Atlas.Provider.Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.0" />
</ItemGroup>
</Project>
77 changes: 72 additions & 5 deletions src/Atlas.Provider.Demo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,101 @@
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
{
public class Program
{
public static void Main(string[] args)
{

}
}

public class BloggingContextFactory : IDesignTimeDbContextFactory<BloggingContext>
{
public BloggingContext CreateDbContext(string[] args)
{
var provider = args.FirstOrDefault();

return new BloggingContext(provider);
}
}
public class BloggingContext : DbContext
{
public DbSet<Blog> 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<Post>()
.HasOne(p => p.Blog)
.WithMany(b => b.Posts)
.HasForeignKey(p => p.BlogUrl)
.HasPrincipalKey(b => b.Url);

modelBuilder.Entity<Blog>()
.HasMany(b => b.Posts)
.WithOne(p => p.Blog)
.HasForeignKey(p => p.BlogUrl);

modelBuilder.Entity<Blog>()
.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<Post> 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; }
}
}

0 comments on commit 8de4b22

Please sign in to comment.