Skip to content

Commit 40a2bed

Browse files
committed
Abled to add custom properties(=columns) into PlayerData table
1 parent f58e1e7 commit 40a2bed

14 files changed

Lines changed: 364 additions & 143 deletions

Source/Migrations/Migrations.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,29 @@
44
using Microsoft.EntityFrameworkCore.Design;
55
using SyncnetPlatform.Databases;
66

7-
public class SyncnetDbContextFactory : IDesignTimeDbContextFactory<SyncnetDbContext>
8-
{
9-
protected HostApplicationBuilder _applicationBuilder;
10-
public SyncnetDbContextFactory() : base()
11-
{
12-
string? environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
7+
//public class SyncnetDbContextFactory : IDesignTimeDbContextFactory<SyncnetDbContext>
8+
//{
9+
// protected HostApplicationBuilder _applicationBuilder;
10+
// public SyncnetDbContextFactory() : base()
11+
// {
12+
// string? environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
1313

14-
_applicationBuilder = Host.CreateApplicationBuilder();
14+
// _applicationBuilder = Host.CreateApplicationBuilder();
1515

16-
_applicationBuilder.Configuration
17-
.SetBasePath(Directory.GetCurrentDirectory())
18-
.AddJsonFile("appsettings.json", false, true)
19-
.AddJsonFile($"appsettings.{environmentName}.json", true, true)
20-
.AddEnvironmentVariables();
21-
}
22-
public SyncnetDbContext CreateDbContext(string[] args)
23-
{
24-
var options = new DbContextOptionsBuilder<SyncnetDbContext>()
25-
.UseNpgsql(_applicationBuilder.Configuration.GetConnectionString("SyncnetPlatform"), options => {
26-
options.MigrationsAssembly("ABMGS.Serverv2.Package");
27-
})
28-
.Options;
16+
// _applicationBuilder.Configuration
17+
// .SetBasePath(Directory.GetCurrentDirectory())
18+
// .AddJsonFile("appsettings.json", false, true)
19+
// .AddJsonFile($"appsettings.{environmentName}.json", true, true)
20+
// .AddEnvironmentVariables();
21+
// }
22+
// public SyncnetDbContext CreateDbContext(string[] args)
23+
// {
24+
// var options = new DbContextOptionsBuilder<SyncnetDbContext>()
25+
// .UseNpgsql(_applicationBuilder.Configuration.GetConnectionString("SyncnetPlatform"), options => {
26+
// options.MigrationsAssembly("Package");
27+
// })
28+
// .Options;
2929

30-
return new SyncnetDbContext(options);
31-
}
32-
}
30+
// return new SyncnetDbContext(options);
31+
// }
32+
//}

Source/Package/Databases/SyncnetDbContext.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,26 @@
55
using System.Text;
66
using Microsoft.EntityFrameworkCore;
77
using Microsoft.EntityFrameworkCore.Infrastructure;
8+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
89
using Npgsql.EntityFrameworkCore.PostgreSQL;
910

1011
namespace SyncnetPlatform.Databases;
1112

12-
public class SyncnetDbContext(DbContextOptions options) : DbContext(options)
13+
public interface IPlayerDataExtendCreater
14+
{
15+
void CreateExtendColumns(EntityTypeBuilder<PlayerData> e);
16+
}
17+
18+
public class SyncnetDbContext(
19+
DbContextOptions options,
20+
IPlayerDataExtendCreater? playerDataExtendCreater = null
21+
) : DbContext(options)
1322
{
1423
public DbSet<PlayerData> Players { get; set; }
1524
public DbSet<PlayerExternalIdentities> ExternalIdentities { get; set; }
1625

26+
27+
1728

1829
protected override void OnModelCreating(ModelBuilder modelBuilder)
1930
{
@@ -30,6 +41,7 @@ protected void OnModelCreating_Player(ModelBuilder modelBuilder)
3041
e.Property(p => p.Id).ValueGeneratedOnAdd();
3142

3243
e.HasIndex(p => p.PlayerId).IsUnique();
44+
playerDataExtendCreater?.CreateExtendColumns(e);
3345
});
3446

3547
}
@@ -48,7 +60,6 @@ protected void OnModelCreating_ExternalIdentities(ModelBuilder modelBuilder)
4860
e.HasIndex(p => new {p.IdProvider, p.IdExternal }).IsUnique();
4961
});
5062
}
51-
5263
}
5364

5465
public enum IdProviderType
@@ -67,6 +78,14 @@ public class PlayerData
6778
public Guid PlayerId { get; set; }
6879
public string PlayerName { get; set; } = string.Empty;
6980
public DateTime Created { get; set; } = DateTime.UtcNow;
81+
82+
private readonly Dictionary<string, object> _playerDataExtend = new();
83+
84+
public object this[string key]
85+
{
86+
get => _playerDataExtend.TryGetValue(key, out var value) ? value : null;
87+
set => _playerDataExtend[key] = value;
88+
}
7089
}
7190

7291

@@ -81,3 +100,4 @@ public class PlayerExternalIdentities
81100

82101
}
83102

103+

Source/Package/Extensions/HostExtension.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ namespace SyncnetPlatform.Extensions;
1010

1111
public static class IHostExtension
1212
{
13-
public static IHost SyncnetDbMigrate<DbContextType>(this IHost host) where DbContextType : SyncnetDbContext
14-
{
15-
using var scope = host.Services.CreateScope();
16-
var db = scope.ServiceProvider.GetRequiredService<DbContextType>();
17-
db.Database.Migrate();
18-
return host;
19-
}
13+
//public static IHost SyncnetDbMigrate<DbContextType>(this IHost host) where DbContextType : SyncnetDbContext
14+
//{
15+
// using var scope = host.Services.CreateScope();
16+
// var db = scope.ServiceProvider.GetRequiredService<DbContextType>();
17+
// db.Database.Migrate();
18+
// return host;
19+
//}
2020
public static IHost SyncnetDbMigrate(this IHost host)
2121
{
2222
using var scope = host.Services.CreateScope();

Source/Package/Extensions/SyncnetPlatformBuilderExtension.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
using SyncnetPlatform.Utils;
3232
using SyncnetPlatform.Utils.Telemetry;
3333
using System.Text;
34+
using System.Reflection;
3435

3536
namespace SyncnetPlatform.Extensions;
3637

@@ -53,9 +54,13 @@ public static void AddSyncnetPlatformSilo(
5354
Action<SyncnetLoggerOption>? LoggerAction = null,
5455
Action<SyncnetTelemetryOption>? TelemetryAction = null)
5556
{
57+
58+
//storing calling assembly name
59+
string? hostAssemblyName = Assembly.GetCallingAssembly()!.GetName().FullName;
60+
5661
AddSyncnetPlatformCommon(builder, LoggerAction, TelemetryAction);
5762
ConfigureOrleansAsSilo(builder);
58-
SyncnetPlatformSiloDbContext(builder);
63+
SyncnetPlatformSiloDbContext(builder, hostAssemblyName);
5964
}
6065
private static void AddSyncnetPlatformCommon(
6166
WebApplicationBuilder builder,
@@ -103,13 +108,14 @@ private static void ConfigureDatabase(WebApplicationBuilder builder)
103108
builder.Services.AddTransient<IPlayerModelRepository, RdbPlayerModelRepository>();
104109
builder.Services.AddTransient<IExternalIdentityRepository, RdbExternalIdentityRepository>();
105110
}
106-
private static void SyncnetPlatformSiloDbContext(WebApplicationBuilder builder)
111+
private static void SyncnetPlatformSiloDbContext(WebApplicationBuilder builder, string? hostAssemblyName)
107112
{
108113
builder.Services.AddDbContextFactory<SyncnetDbContext>(opt =>
109114
{
110115
opt.UseNpgsql(builder.Configuration.GetConnectionString("postgres"), optionBuilder =>
111116
{
112-
optionBuilder.MigrationsAssembly(typeof(SyncnetDbContext).Assembly.FullName);
117+
//optionBuilder.MigrationsAssembly(typeof(SyncnetDbContext).Assembly.FullName);
118+
optionBuilder.MigrationsAssembly(hostAssemblyName);
113119
});
114120
});
115121
}

Source/Package/Package.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343

4444
<ItemGroup>
4545
<Folder Include="Authentication\Guest\" />
46-
<Folder Include="Migrations\" />
4746
<Folder Include="Exceptions\" />
4847
</ItemGroup>
4948
<Target Name="FlatcGenerate" BeforeTargets="BeforeCompile">

Source/Package/Repositories/RdbPlayerModelRepository.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,23 @@ public interface IExternalIdentityRepository
7373
public class RdbExternalIdentityRepository : IExternalIdentityRepository
7474
{
7575
private readonly ILogger<RdbExternalIdentityRepository> _logger;
76-
private readonly SyncnetDbContext _db;
76+
private readonly IDbContextFactory<SyncnetDbContext> _dbContextFactory;
77+
78+
//private readonly SyncnetDbContext _db;
7779

7880
public RdbExternalIdentityRepository(
7981
ILogger<RdbExternalIdentityRepository> logger,
80-
SyncnetDbContext db
82+
IDbContextFactory<SyncnetDbContext> dbContextFactory
8183

8284
)
8385
{
8486
_logger = logger;
85-
_db = db;
87+
_dbContextFactory = dbContextFactory;
8688
}
8789
public async Task<Guid> GetOrCreate(IdProviderType idProviderType, string idExternal)
8890
{
89-
PlayerExternalIdentities? entity = await _db.ExternalIdentities
91+
var dbContext = _dbContextFactory.CreateDbContext();
92+
PlayerExternalIdentities? entity = await dbContext.ExternalIdentities
9093
.SingleOrDefaultAsync(p =>
9194
p.IdProvider == idProviderType &&
9295
p.IdExternal == idExternal);
@@ -102,15 +105,15 @@ public async Task<Guid> GetOrCreate(IdProviderType idProviderType, string idExte
102105
SyncnetId = Guid.NewGuid(),
103106
Created = DateTime.UtcNow
104107
};
105-
await _db.ExternalIdentities.AddAsync(entity);
106-
await _db.SaveChangesAsync();
108+
await dbContext.ExternalIdentities.AddAsync(entity);
109+
await dbContext.SaveChangesAsync();
107110
}
108111
catch(DbUpdateException ex) when (ex.InnerException is PostgresException pg)
109112
{
110113
if(pg.SqlState == PostgresErrorCodes.UniqueViolation)
111114
{
112115
// Reload the entity.
113-
entity = await _db.ExternalIdentities
116+
entity = await dbContext.ExternalIdentities
114117
.SingleAsync(p =>
115118
p.IdProvider == idProviderType &&
116119
p.IdExternal == idExternal);
@@ -128,7 +131,9 @@ public async Task<Guid> GetOrCreate(IdProviderType idProviderType, string idExte
128131

129132
public async Task Remove(IdProviderType idProviderType, string idExternal)
130133
{
131-
_ = await _db.ExternalIdentities
134+
var dbContext = _dbContextFactory.CreateDbContext();
135+
136+
_ = await dbContext.ExternalIdentities
132137
.Where(w =>
133138
w.IdProvider == idProviderType &&
134139
w.IdExternal == idExternal)

Source/Package/Migrations/20260305034242_Init.Designer.cs renamed to Source/Silo/Migrations/20260611054156_Init.Designer.cs

Lines changed: 15 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/Package/Migrations/20260305034242_Init.cs renamed to Source/Silo/Migrations/20260611054156_Init.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#nullable disable
66

7-
namespace SyncnetPlatform.Migrations
7+
namespace Silo.Migrations
88
{
99
/// <inheritdoc />
1010
public partial class Init : Migration
@@ -20,7 +20,11 @@ protected override void Up(MigrationBuilder migrationBuilder)
2020
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
2121
PlayerId = table.Column<Guid>(type: "uuid", nullable: false),
2222
PlayerName = table.Column<string>(type: "text", nullable: false),
23-
Created = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
23+
Created = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
24+
Level = table.Column<int>(type: "integer", nullable: false),
25+
PosX = table.Column<float>(type: "real", nullable: false),
26+
PosY = table.Column<float>(type: "real", nullable: false),
27+
Title = table.Column<string>(type: "text", nullable: true)
2428
},
2529
constraints: table =>
2630
{

0 commit comments

Comments
 (0)