You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.6 KiB

using EF_ChangeLog.Controller;
using EF_ChangeLog.Model;
namespace EF_ChangeLog
{
internal class Program
{
static void Main(string[] args)
{
using (var context = new StockDbContext())
{
// Create samples
Random rand = new Random();
List<Stock> newStocks = new List<Stock>()
{
new Stock() { Name = $"Test Stock{rand.Next(1, 1000)}", Price = 1000 },
new Stock() { Name = $"Test Stock{rand.Next(1, 1000)}", Price = 1000 },
new Stock() { Name = $"Test Stock{rand.Next(1, 1000)}", Price = 1000 },
new Stock() { Name = $"Test Stock{rand.Next(1, 1000)}", Price = 1000 }
};
foreach (var newStock in newStocks)
{
newStock.SaveSnapshot();
context.Stocks.Add(newStock);
}
context.SaveChanges();
// Update sample
var stock = context.Stocks.Find(rand.Next(1, 5));
if (stock != null)
{
stock.Price = rand.Next(1, 10) * 1000;
stock.SaveSnapshot();
context.SaveChanges();
}
// Delete sample
var del = context.Stocks.OrderByDescending(s => s.Id).FirstOrDefault();
if (del != null)
{
context.Stocks.Remove(del);
del.SaveSnapshot();
context.SaveChanges();
}
}
}
}
}