fix
This commit is contained in:
@@ -115,6 +115,10 @@ public class AppDbContext : DbContext
|
||||
.HasOne(x => x.Currency)
|
||||
.WithMany(x => x.Rates)
|
||||
.HasForeignKey(x => x.CurrencyId);
|
||||
|
||||
modelBuilder.Entity<Currency>()
|
||||
.HasOne(x => x.CurrentRate)
|
||||
.WithOne(x => x.Currency);
|
||||
}
|
||||
|
||||
private void AccountCreating(ModelBuilder modelBuilder)
|
||||
|
||||
@@ -17,9 +17,11 @@ public class RepositoryInitializer
|
||||
ConnectionString = connectionString;
|
||||
|
||||
var db = AppDbContextFactory.Instance.CreateDbContext();
|
||||
db.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
|
||||
db.Database.Migrate();
|
||||
|
||||
Prefill(db);
|
||||
|
||||
UpdateCurrentRates(db);
|
||||
}
|
||||
|
||||
public static ConnectionConfiguration ConnectionString { get; internal set; } = null!;
|
||||
@@ -52,4 +54,28 @@ public class RepositoryInitializer
|
||||
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
private static void UpdateCurrentRates(AppDbContext dbContext)
|
||||
{
|
||||
var lastrates = dbContext.CurrencyRates
|
||||
.Include(x => x.Currency)
|
||||
.GroupBy(x => new
|
||||
{
|
||||
x.CurrencyId,
|
||||
}, (key, g) => g.OrderByDescending(x => x.DateTime).First())
|
||||
.ToList();
|
||||
|
||||
var currencies = dbContext.Currencies.ToList();
|
||||
foreach (var currency in currencies)
|
||||
{
|
||||
var rate = lastrates.FirstOrDefault(x => x.CurrencyId == currency.Id);
|
||||
if (rate != null)
|
||||
{
|
||||
//currency.CurrentRateId = rate.Id;
|
||||
dbContext.Attach(currency);
|
||||
}
|
||||
}
|
||||
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user