ShopPlatform.Web
1.0.0.0
농산물, 소프트웨어 라이선스와 개발 용역을 직접 판매하는 CodeMaru 직영 쇼핑몰입니다.
Toggle main menu visibility
로딩중...
검색중...
일치하는것 없음
TenantDbContextFactory.cs
이 파일의 문서화 페이지로 가기
1
using
Microsoft.EntityFrameworkCore;
2
using
ShopPlatform.Models
;
3
4
namespace
ShopPlatform.Data
;
5
14
public
sealed
class
TenantDbContextFactory
15
{
24
private
readonly
ShopOptions
_opts
;
33
private
readonly ILogger<TenantDbContextFactory>
_logger
;
34
59
public
TenantDbContextFactory
(
ShopOptions
opts, ILogger<TenantDbContextFactory> logger)
60
{
61
_opts
= opts;
62
_logger
= logger;
63
}
64
89
public
TenantDbContext
Create
(
string
slug)
90
{
91
var dbPath =
GetDbPath
(slug);
92
Directory.CreateDirectory(Path.GetDirectoryName(dbPath)!);
93
94
var opts =
new
DbContextOptionsBuilder<TenantDbContext>()
95
.UseSqlite($
"Data Source={dbPath}"
)
96
.Options;
97
98
var ctx =
new
TenantDbContext
(opts);
99
ctx.Database.EnsureCreated();
100
ApplyColumnMigrations
(ctx);
101
return
ctx;
102
}
103
120
private
void
ApplyColumnMigrations
(
TenantDbContext
ctx)
121
{
122
// Products 테이블 컬럼 목록 조회
123
var existingColumns = ctx.Database
124
.SqlQueryRaw<
string
>(
"SELECT name FROM pragma_table_info('Products')"
)
125
.ToHashSet(StringComparer.OrdinalIgnoreCase);
126
127
if
(!existingColumns.Contains(
"IsFeatured"
))
128
{
129
ctx.Database.ExecuteSqlRaw(
130
"ALTER TABLE Products ADD COLUMN IsFeatured INTEGER NOT NULL DEFAULT 0"
);
131
_logger
.LogInformation(
"Migration: Products.IsFeatured 컬럼 추가"
);
132
}
133
134
if
(!existingColumns.Contains(
"SortOrder"
))
135
{
136
ctx.Database.ExecuteSqlRaw(
137
"ALTER TABLE Products ADD COLUMN SortOrder INTEGER NOT NULL DEFAULT 0"
);
138
_logger
.LogInformation(
"Migration: Products.SortOrder 컬럼 추가"
);
139
}
140
141
if
(!existingColumns.Contains(
"DetailHtml"
))
142
{
143
ctx.Database.ExecuteSqlRaw(
144
"ALTER TABLE Products ADD COLUMN DetailHtml TEXT NOT NULL DEFAULT ''"
);
145
_logger
.LogInformation(
"Migration: Products.DetailHtml 컬럼 추가"
);
146
}
147
148
if
(!existingColumns.Contains(
"VideoUrl"
))
149
{
150
ctx.Database.ExecuteSqlRaw(
151
"ALTER TABLE Products ADD COLUMN VideoUrl TEXT NOT NULL DEFAULT ''"
);
152
_logger
.LogInformation(
"Migration: Products.VideoUrl 컬럼 추가"
);
153
}
154
155
if
(!existingColumns.Contains(
"IsUnlimitedStock"
))
156
{
157
ctx.Database.ExecuteSqlRaw(
158
"ALTER TABLE Products ADD COLUMN IsUnlimitedStock INTEGER NOT NULL DEFAULT 0"
);
159
_logger
.LogInformation(
"Migration: Products.IsUnlimitedStock 컬럼 추가"
);
160
}
161
162
if
(!existingColumns.Contains(
"DetailImagesJson"
))
163
{
164
ctx.Database.ExecuteSqlRaw(
165
"ALTER TABLE Products ADD COLUMN DetailImagesJson TEXT NOT NULL DEFAULT '[]'"
);
166
_logger
.LogInformation(
"Migration: Products.DetailImagesJson 컬럼 추가"
);
167
}
168
}
169
194
public
string
GetDbPath
(
string
slug)
195
=> Path.Combine(
_opts
.ResolvedDataPath, slug,
"shop.db"
);
196
}
ShopPlatform.Data
Definition
ShopSeeder.cs:4
ShopPlatform.Models
Definition
CartItem.cs:1
ShopPlatform.Data.TenantDbContext
Definition
TenantDbContext.cs:15
ShopPlatform.Data.TenantDbContextFactory.TenantDbContextFactory
TenantDbContextFactory(ShopOptions opts, ILogger< TenantDbContextFactory > logger)
Definition
TenantDbContextFactory.cs:59
ShopPlatform.Data.TenantDbContextFactory._opts
readonly ShopOptions _opts
Definition
TenantDbContextFactory.cs:24
ShopPlatform.Data.TenantDbContextFactory.Create
TenantDbContext Create(string slug)
Definition
TenantDbContextFactory.cs:89
ShopPlatform.Data.TenantDbContextFactory.GetDbPath
string GetDbPath(string slug)
Definition
TenantDbContextFactory.cs:194
ShopPlatform.Data.TenantDbContextFactory.ApplyColumnMigrations
void ApplyColumnMigrations(TenantDbContext ctx)
Definition
TenantDbContextFactory.cs:120
ShopPlatform.Data.TenantDbContextFactory._logger
readonly ILogger< TenantDbContextFactory > _logger
Definition
TenantDbContextFactory.cs:33
ShopPlatform.Models.ShopOptions
Definition
ShopOptions.cs:12
Data
TenantDbContextFactory.cs
다음에 의해 생성됨 :
1.17.0