Backup / Restore funktioner infört för kommande uppdateringar
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user