Backup / Restore funktioner infört för kommande uppdateringar

This commit is contained in:
2021-03-07 15:57:33 +01:00
parent c2d8e76643
commit 03878ae8af
7 changed files with 167 additions and 33 deletions

View File

@ -64,6 +64,13 @@ namespace StockDAL
return output;
}
public void RemoveAllStocks()
{
using var context = new StockContext();
context.Stocks.RemoveRange(GetAllStocks());
context.SaveChanges();
}
public void InsertMany(List<StockMember> stockMembers)
{
using var context = new StockContext();
@ -71,5 +78,38 @@ namespace StockDAL
context.SaveChanges();
}
public void RestoreStockMembers(List<StockMember> stockMembers)
{
using var context = new StockContext();
using var transaction = context.Database.BeginTransaction();
try
{
var stocksToRemove = context.Stocks;
context.Stocks.RemoveRange(stocksToRemove);
var insertStocks = stockMembers.Select(o => new StockMember
{
Id = 0,
StockId = o.StockId,
StockExtId = o.StockExtId,
BuyDate = o.BuyDate,
BuyValue = o.BuyValue,
ActAmount = o.ActAmount,
ActValue = o.ActValue,
ActDate = o.ActDate,
SoldValue = o.SoldValue,
SoldDate = o.SoldDate,
Comment = o.Comment,
PostAmount = o.PostAmount
}).ToList();
context.Stocks.AddRange(insertStocks);
context.SaveChanges();
transaction.Commit();
}
catch (Exception ex)
{
throw new InvalidOperationException(ex.Message);
}
}
}
}