Compare commits
1 Commits
small-fixe
...
experiment
Author | SHA1 | Date | |
---|---|---|---|
5189a8e6dd |
BTCPayServer.Data
BTCPayServer
@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
namespace BTCPayServer.Data
|
||||
{
|
||||
@ -11,23 +12,24 @@ namespace BTCPayServer.Data
|
||||
{
|
||||
public ApplicationDbContext CreateDbContext(string[] args)
|
||||
{
|
||||
|
||||
var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
|
||||
builder.UseNpgsql("User ID=postgres;Host=127.0.0.1;Port=39372;Database=designtimebtcpay");
|
||||
|
||||
builder.UseSqlite("Data Source=temp.db");
|
||||
|
||||
return new ApplicationDbContext(builder.Options, true);
|
||||
return new ApplicationDbContext(builder.Options)
|
||||
{
|
||||
_designTime = true
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
|
||||
{
|
||||
private readonly bool _designTime;
|
||||
public bool _designTime;
|
||||
|
||||
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, bool designTime = false)
|
||||
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
|
||||
: base(options)
|
||||
{
|
||||
_designTime = designTime;
|
||||
_designTime = false;
|
||||
}
|
||||
|
||||
public DbSet<AddressInvoiceData> AddressInvoices { get; set; }
|
||||
@ -118,17 +120,48 @@ namespace BTCPayServer.Data
|
||||
// This only supports millisecond precision, but should be sufficient for most use cases.
|
||||
foreach (var entityType in builder.Model.GetEntityTypes())
|
||||
{
|
||||
var properties = entityType.ClrType.GetProperties().Where(p => p.PropertyType == typeof(DateTimeOffset));
|
||||
var properties = entityType.ClrType.GetProperties()
|
||||
.Where(p => p.PropertyType == typeof(DateTimeOffset));
|
||||
foreach (var property in properties)
|
||||
{
|
||||
builder
|
||||
.Entity(entityType.Name)
|
||||
.Property(property.Name)
|
||||
.HasConversion(new Microsoft.EntityFrameworkCore.Storage.ValueConversion.DateTimeOffsetToBinaryConverter());
|
||||
.HasConversion(
|
||||
new Microsoft.EntityFrameworkCore.Storage.ValueConversion.
|
||||
DateTimeOffsetToBinaryConverter());
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var property in builder.Model.GetEntityTypes()
|
||||
.SelectMany(t => t.GetProperties())
|
||||
.Where(p => p.ClrType == typeof(DateTimeOffset)))
|
||||
{
|
||||
property.SetValueConverter(
|
||||
new ValueConverter<DateTimeOffset, DateTime>(
|
||||
convertToProviderExpression: dateTimeOffset => dateTimeOffset.UtcDateTime,
|
||||
convertFromProviderExpression: dateTime => new DateTimeOffset(dateTime)
|
||||
));
|
||||
}
|
||||
|
||||
foreach (var property in builder.Model.GetEntityTypes()
|
||||
.SelectMany(t => t.GetProperties())
|
||||
.Where(p => p.ClrType == typeof(DateTimeOffset?)))
|
||||
{
|
||||
property.SetValueConverter(
|
||||
new ValueConverter<DateTimeOffset?, DateTime?>(
|
||||
convertToProviderExpression: dateTimeOffset =>
|
||||
DateTime.SpecifyKind(dateTimeOffset.GetValueOrDefault().UtcDateTime,
|
||||
DateTimeKind.Utc),
|
||||
convertFromProviderExpression: dateTime =>
|
||||
new DateTimeOffset(dateTime.GetValueOrDefault())
|
||||
));
|
||||
|
||||
//property.SetColumnType(nameof(Nullable<DateTime>));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -51,6 +51,7 @@
|
||||
<PackageReference Include="BundlerMinifier.Core" Version="3.2.435" />
|
||||
<PackageReference Include="BundlerMinifier.TagHelpers" Version="3.2.435" />
|
||||
<PackageReference Include="CsvHelper" Version="15.0.5" />
|
||||
<PackageReference Include="EFCoreSecondLevelCacheInterceptor" Version="3.2.0" />
|
||||
<PackageReference Include="Fido2" Version="2.0.1" />
|
||||
<PackageReference Include="Fido2.AspNet" Version="2.0.1" />
|
||||
<PackageReference Include="HtmlSanitizer" Version="5.0.372" />
|
||||
|
@ -36,6 +36,7 @@ using BTCPayServer.Services.Rates;
|
||||
using BTCPayServer.Services.Stores;
|
||||
using BTCPayServer.Services.Wallets;
|
||||
using BundlerMinifier.TagHelpers;
|
||||
using EFCoreSecondLevelCacheInterceptor;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
@ -69,11 +70,18 @@ namespace BTCPayServer.Hosting
|
||||
}
|
||||
public static IServiceCollection AddBTCPayServer(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddEFSecondLevelCache(options =>
|
||||
options
|
||||
.UseMemoryCacheProvider()
|
||||
.DisableLogging(false)
|
||||
.UseCacheKeyPrefix("EF_")
|
||||
.CacheAllQueries(CacheExpirationMode.Absolute, TimeSpan.FromMinutes(30)));
|
||||
services.AddSingleton<MvcNewtonsoftJsonOptions>(o => o.GetRequiredService<IOptions<MvcNewtonsoftJsonOptions>>().Value);
|
||||
services.AddDbContext<ApplicationDbContext>((provider, o) =>
|
||||
services.AddDbContextPool<ApplicationDbContext>((provider, o) =>
|
||||
{
|
||||
var factory = provider.GetRequiredService<ApplicationDbContextFactory>();
|
||||
factory.ConfigureBuilder(o);
|
||||
o.AddInterceptors(provider.GetRequiredService<SecondLevelCacheInterceptor>());
|
||||
});
|
||||
services.AddHttpClient();
|
||||
services.AddHttpClient(nameof(ExplorerClientProvider), httpClient =>
|
||||
|
Reference in New Issue
Block a user