-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src/demo: add types, foreign keys ... (#9)
- Loading branch information
1 parent
3367e43
commit 8de4b22
Showing
3 changed files
with
76 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |