public void SaveToDatabase() { using (var ndb = new MyDbContext()) { db.AddRange(RomsToAdd); db.UpdateRange(RomsToUpdate); db.AddRange(DisksToAdd); db.UpdateRange(DisksToUpdate); db.SaveChanges(); GamesLibData.ForceUpdate(); } }
public static void SaveToDatabase() { using (db) { db.AddRange(RomsToAdd); db.UpdateRange(RomsToUpdate); db.AddRange(DisksToAdd); db.UpdateRange(DisksToUpdate); db.SaveChanges(); //GamesLibData.ForceUpdate(); } }
/// <summary> /// Adds current cart to the db. Takes orders from the cart and passes them individually to the db. /// </summary> /// <param name="currentCart"></param> /// <param name="_db"></param> public static void AddCartToDb(Cart currentCart, MyDbContext _db) { var orderList = new List <Order>(); int currCartId; if (!_db.Orders.Any())//if this is the first order in the DB, set cart ID to 1 { currCartId = 1; } else { var prevMaxCartId = _db.Orders.Select(x => x.CartId).ToList();//get the next available cartID currCartId = prevMaxCartId.Max() + 1; } for (int i = 0; i < currentCart.ProdIds.Count(); i++) { for (int j = 0; j < currentCart.OrderQuantity[i]; j++)//adds a new Order with all properties for each quantity purchased { orderList.Add(new Order { CustomerId = currentCart.CustId, LocationId = currentCart.LocIds[i], ProductId = currentCart.ProdIds[i], Price = currentCart.Costs[i], CheckoutTime = DateTime.Now, CartId = currCartId }); var x = _db.Products.Find(currentCart.ProdIds[i]); x.Quantity--;//subtracting out the quantity in inventory for each item purchased _db.SaveChanges(); } } _db.AddRange(orderList); _db.SaveChanges(); return; }
public void TestDemo() { //SETUP var options = SqliteInMemory.CreateOptions <MyDbContext>(); using var context = new MyDbContext(options); context.Database.EnsureCreated(); var e1 = new MyEntity { SoftDeleted = false }; var e2 = new MyEntity { SoftDeleted = true }; context.AddRange(e1, e2); context.SaveChanges(); //ATTEMPT var query = RelationalQueryableExtensions.FromSqlRaw(context.MyEntities, "SELECT * FROM MyEntities"); var entities = query.ToList(); //VERIFY var sql = query.ToQueryString(); _output.WriteLine(sql); entities.Count().ShouldEqual(1); }
public void TestTwoQueryFilters() { //SETUP var options = SqliteInMemory.CreateOptions <MyDbContext>(); var userId = "GoodId"; using var context = new MyDbContext(options, userId); context.Database.EnsureCreated(); //ATTEMPT var e1 = new MyEntity { SoftDeleted = false, UserId = userId }; var e2 = new MyEntity { SoftDeleted = true, UserId = userId }; var e3 = new MyEntity { SoftDeleted = false, UserId = "BadId" }; context.AddRange(e1, e2, e3); context.SaveChanges(); //VERIFY context.ChangeTracker.Clear(); context.MyEntities.Count().ShouldEqual(1); context.MyEntities.IgnoreQueryFilters().Count().ShouldEqual(3); }
static void Main(string[] args) { using (var db = new MyDbContext()) { if (!db.Articles.Any()) { var articles = new List <Article> { new Article { Title = "testing is ok", Abst = "this is a test about postgre full text searching" }, new Article { Title = "tested all bugs", Abst = "there is no bug exists in this app" } }; db.AddRange(articles); db.SaveChanges(); } #region 英文 var query = "test"; var data = db.Articles .Where(p => p.TitleVector.Matches(query) || p.AbstVector.Matches(query)) .OrderByDescending(p => p.TitleVector.Rank(EF.Functions.ToTsQuery(query)) * 2.0 + p.AbstVector.Rank(EF.Functions.ToTsQuery(query))) .Select(p => new Article { Title = p.Title, Abst = p.Abst, TitleHL = EF.Functions.ToTsQuery(query).GetResultHeadline(p.Title), AbstHL = EF.Functions.ToTsQuery(query).GetResultHeadline(p.Abst), }); #endregion #region 中文 postgre未安装中文分词zhparser插件 //var query = "测试"; //var config = "chinese_zh"; //var data = db.Articles // .Where(p => p.TitleVector.Matches(EF.Functions.ToTsQuery(config, query)) || // p.AbstVector.Matches(EF.Functions.ToTsQuery(config, query))) // .OrderByDescending(p => p.TitleVector.Rank(EF.Functions.ToTsQuery(config, query)) * 2.0 + // p.AbstVector.Rank(EF.Functions.ToTsQuery(config, query))) // .Select(p => new Article // { // Title = p.Title, // Abst = p.Abst, // TitleHL = EF.Functions.ToTsQuery(config, query).GetResultHeadline(config, p.Title, ""), // AbstHL = EF.Functions.ToTsQuery(config, query).GetResultHeadline(config, p.Abst, ""), // }); #endregion foreach (var article in data) { Console.WriteLine($"{article.Title}\t{article.Abst}\t{article.TitleHL}\t{article.AbstHL}"); } } }
public async Task SaveJsonPostsInDatabase() { var jsonData = System.IO.File.ReadAllText(@"Secret-seed-blog.json"); JsonSerializerSettings settings = new JsonSerializerSettings { ContractResolver = new PrivateSetterContractResolver() }; List <Post> posts = JsonConvert.DeserializeObject <List <Post> >(jsonData, settings); _dbContext.AddRange(posts); if (_dbContext.ChangeTracker.HasChanges()) { await _dbContext.SaveChangesAsync(); } }
public static void SeedDataContext(this MyDbContext context) { PasswordHasher passwordHasher = new PasswordHasher(); var user = new User() { Username = "******", FullName = "Cinema Admin", Password = passwordHasher.HashPassword("admin@123456"), Email = "*****@*****.**", UserRoles = new List <UserRole>() { new UserRole() { Role = new Role() { Name = "Admin", } }, new UserRole() { Role = new Role() { Name = "User", } }, new UserRole() { Role = new Role() { Name = "Staff", } } }, }; if (context.Users.Any(u => u.Username == "cinema-admin") == false) { context.AddRange(user); context.SaveChanges(); } }
public void TestGetLocationList() { var options = new DbContextOptionsBuilder <MyDbContext>().UseInMemoryDatabase(databaseName: "TestGetCartTotal").Options; using (var context = new MyDbContext(options)) { var stores = new List <Store> { new Store { Location = "Irving" }, new Store { Location = "Plano" }, new Store { Location = "Arlington" }, new Store { Location = "Lewisville" }, new Store { Location = "Mesquite" }, }; context.AddRange(stores); context.SaveChanges(); var numList = new List <int>(); numList.Add(1); numList.Add(2); numList.Add(3); numList.Add(4); numList.Add(5); List <string> anslist = BusinessLogic.GetLocationNameList(numList, context); Assert.Equal(stores[0].Location, anslist[0]); } }
public void TestDemo() { //SETUP var options = SqliteInMemory.CreateOptions <MyDbContext>(); using var context = new MyDbContext(options); context.Database.EnsureCreated(); //ATTEMPT var e1 = new MyEntity { SoftDeleted = false }; var e2 = new MyEntity { SoftDeleted = true }; context.AddRange(e1, e2); context.SaveChanges(); //VERIFY context.ChangeTracker.Clear(); context.MyEntities.Count().ShouldEqual(1); context.MyEntities.IgnoreQueryFilters().Count().ShouldEqual(2); }
public static void SeedStores(MyDbContext context) { var stores = new List <Store> { new Store { Location = "Irving" }, new Store { Location = "Plano" }, new Store { Location = "Arlington" }, new Store { Location = "Lewisville" }, new Store { Location = "Mesquite" }, }; context.AddRange(stores); context.SaveChanges(); }
public void TestSubcartFormatCount() { int numOfOrderGroups = -1; var options = new DbContextOptionsBuilder <MyDbContext>().UseInMemoryDatabase(databaseName: "TestSubcartFormatCount").Options; using (var context = new MyDbContext(options)) { var productList = new List <Product>() { new Product { Name = "Mattress Frame", Description = "This is a mattress frame to fit a king-sized mattress.", Price = 149.99, Quantity = 50, StoreId = 1 }, new Product { Name = "Memory Foam Mattress", Description = "This is a king-sized memory foam mattress. The best of the best!", Price = 499.99, Quantity = 50, StoreId = 2 }, new Product { Name = "Spring Mattress", Description = "This is a king-sized spring mattress. Comfort at an affordable price!", Price = 274.99, Quantity = 50, StoreId = 3 }, }; context.Products.AddRange(productList); context.SaveChanges(); var stores = new List <Store> { new Store { Location = "Irving" }, new Store { Location = "Plano" }, new Store { Location = "Arlington" }, new Store { Location = "Lewisville" }, new Store { Location = "Mesquite" }, }; context.AddRange(stores); context.SaveChanges(); Cart newCart = BusinessLogic.NewCart(); newCart.CustId = 1; newCart.Costs.Add(100); newCart.Costs.Add(200); newCart.Costs.Add(300); newCart.CustId = 1; newCart.LocIds.Add(1); newCart.LocIds.Add(2); newCart.LocIds.Add(3); newCart.OrderQuantity.Add(3); newCart.OrderQuantity.Add(2); newCart.OrderQuantity.Add(1); newCart.ProdIds.Add(1); newCart.ProdIds.Add(2); newCart.ProdIds.Add(3); newCart.ProdNames.Add("a"); newCart.ProdNames.Add("b"); newCart.ProdNames.Add("c"); newCart.TotalCost = 0.00; BusinessLogic.AddCartToDb(newCart, context); BusinessLogic.AddCartToDb(newCart, context); List <HistorySubcart> historySubcart = BusinessLogic.FormatLocOrderHistory(1, context); numOfOrderGroups = historySubcart.Count(); } Assert.Equal(2, numOfOrderGroups); }
public void AddMultiple(List <T> entities) { _context.AddRange(entities); }
public void AddRange(IEnumerable entities) { _context.AddRange(entities); // Save(); }
public virtual void InserirLote(List <T> lista) { _Db.AddRange(lista); _Db.SaveChanges(); }
public static void SeedProducts(MyDbContext context) { var prod = new List <Product> { new Product { Name = "Mattress Frame", Description = "This is a mattress frame to fit a king-sized mattress.", Price = 149.99, Quantity = 50, StoreId = 1 }, new Product { Name = "Memory Foam Mattress", Description = "This is a king-sized memory foam mattress. The best of the best!", Price = 499.99, Quantity = 50, StoreId = 1 }, new Product { Name = "Spring Mattress", Description = "This is a king-sized spring mattress. Comfort at an affordable price!", Price = 274.99, Quantity = 50, StoreId = 1 }, new Product { Name = "Box Spring", Description = "This is a must for all king-sized beds!", Price = 89.99, Quantity = 50, StoreId = 1 }, new Product { Name = "Pillow (Feather)", Description = "This is an extremely comfortable feather pillow!", Price = 24.99, Quantity = 50, StoreId = 1 }, new Product { Name = "Pillow (Microbead)", Description = "This is a modern form-fitting pillow!", Price = 22.99, Quantity = 50, StoreId = 1 }, new Product { Name = "Pillow (Memory Foam)", Description = "This memory foam pillow will give you the ultimate support!", Price = 32.99, Quantity = 50, StoreId = 1 }, new Product { Name = "Comforter", Description = "This comforter will fit a king size mattress.", Price = 28.99, Quantity = 50, StoreId = 1 }, new Product { Name = "Sheets", Description = "This set of sheets will fit a king-sized bed.", Price = 42.99, Quantity = 50, StoreId = 1 }, new Product { Name = "Mattress Frame", Description = "This is a mattress frame to fit a king-sized mattress.", Price = 149.99, Quantity = 50, StoreId = 2 }, new Product { Name = "Memory Foam Mattress", Description = "This is a king-sized memory foam mattress. The best of the best!", Price = 499.99, Quantity = 50, StoreId = 2 }, new Product { Name = "Spring Mattress", Description = "This is a king-sized spring mattress. Comfort at an affordable price!", Price = 274.99, Quantity = 50, StoreId = 2 }, new Product { Name = "Box Spring", Description = "This is a must for all king-sized beds!", Price = 89.99, Quantity = 50, StoreId = 2 }, new Product { Name = "Pillow (Feather)", Description = "This is an extremely comfortable feather pillow!", Price = 24.99, Quantity = 50, StoreId = 2 }, new Product { Name = "Pillow (Microbead)", Description = "This is a modern form-fitting pillow!", Price = 22.99, Quantity = 50, StoreId = 2 }, new Product { Name = "Pillow (Memory Foam)", Description = "This memory foam pillow will give you the ultimate support!", Price = 32.99, Quantity = 50, StoreId = 2 }, new Product { Name = "Comforter", Description = "This comforter will fit a king size mattress.", Price = 28.99, Quantity = 50, StoreId = 2 }, new Product { Name = "Sheets", Description = "This set of sheets will fit a king-sized bed.", Price = 42.99, Quantity = 50, StoreId = 2 }, new Product { Name = "Mattress Frame", Description = "This is a mattress frame to fit a king-sized mattress.", Price = 149.99, Quantity = 50, StoreId = 3 }, new Product { Name = "Memory Foam Mattress", Description = "This is a king-sized memory foam mattress. The best of the best!", Price = 499.99, Quantity = 50, StoreId = 3 }, new Product { Name = "Spring Mattress", Description = "This is a king-sized spring mattress. Comfort at an affordable price!", Price = 274.99, Quantity = 50, StoreId = 3 }, new Product { Name = "Box Spring", Description = "This is a must for all king-sized beds!", Price = 89.99, Quantity = 50, StoreId = 3 }, new Product { Name = "Pillow (Feather)", Description = "This is an extremely comfortable feather pillow!", Price = 24.99, Quantity = 50, StoreId = 3 }, new Product { Name = "Pillow (Microbead)", Description = "This is a modern form-fitting pillow!", Price = 22.99, Quantity = 50, StoreId = 3 }, new Product { Name = "Pillow (Memory Foam)", Description = "This memory foam pillow will give you the ultimate support!", Price = 32.99, Quantity = 50, StoreId = 3 }, new Product { Name = "Comforter", Description = "This comforter will fit a king size mattress.", Price = 28.99, Quantity = 50, StoreId = 3 }, new Product { Name = "Sheets", Description = "This set of sheets will fit a king-sized bed.", Price = 42.99, Quantity = 50, StoreId = 3 }, new Product { Name = "Mattress Frame", Description = "This is a mattress frame to fit a king-sized mattress.", Price = 149.99, Quantity = 50, StoreId = 4 }, new Product { Name = "Memory Foam Mattress", Description = "This is a king-sized memory foam mattress. The best of the best!", Price = 499.99, Quantity = 50, StoreId = 4 }, new Product { Name = "Spring Mattress", Description = "This is a king-sized spring mattress. Comfort at an affordable price!", Price = 274.99, Quantity = 50, StoreId = 4 }, new Product { Name = "Box Spring", Description = "This is a must for all king-sized beds!", Price = 89.99, Quantity = 50, StoreId = 4 }, new Product { Name = "Pillow (Feather)", Description = "This is an extremely comfortable feather pillow!", Price = 24.99, Quantity = 50, StoreId = 4 }, new Product { Name = "Pillow (Microbead)", Description = "This is a modern form-fitting pillow!", Price = 22.99, Quantity = 50, StoreId = 4 }, new Product { Name = "Pillow (Memory Foam)", Description = "This memory foam pillow will give you the ultimate support!", Price = 32.99, Quantity = 50, StoreId = 4 }, new Product { Name = "Comforter", Description = "This comforter will fit a king size mattress.", Price = 28.99, Quantity = 50, StoreId = 4 }, new Product { Name = "Sheets", Description = "This set of sheets will fit a king-sized bed.", Price = 42.99, Quantity = 50, StoreId = 4 }, new Product { Name = "Mattress Frame", Description = "This is a mattress frame to fit a king-sized mattress.", Price = 149.99, Quantity = 50, StoreId = 5 }, new Product { Name = "Memory Foam Mattress", Description = "This is a king-sized memory foam mattress. The best of the best!", Price = 499.99, Quantity = 50, StoreId = 5 }, new Product { Name = "Spring Mattress", Description = "This is a king-sized spring mattress. Comfort at an affordable price!", Price = 274.99, Quantity = 50, StoreId = 5 }, new Product { Name = "Box Spring", Description = "This is a must for all king-sized beds!", Price = 89.99, Quantity = 50, StoreId = 5 }, new Product { Name = "Pillow (Feather)", Description = "This is an extremely comfortable feather pillow!", Price = 24.99, Quantity = 50, StoreId = 5 }, new Product { Name = "Pillow (Microbead)", Description = "This is a modern form-fitting pillow!", Price = 22.99, Quantity = 50, StoreId = 5 }, new Product { Name = "Pillow (Memory Foam)", Description = "This memory foam pillow will give you the ultimate support!", Price = 32.99, Quantity = 50, StoreId = 5 }, new Product { Name = "Comforter", Description = "This comforter will fit a king size mattress.", Price = 28.99, Quantity = 50, StoreId = 5 }, new Product { Name = "Sheets", Description = "This set of sheets will fit a king-sized bed.", Price = 42.99, Quantity = 50, StoreId = 5 }, }; context.AddRange(prod); context.SaveChanges(); }