public void Seed() { _ctx.Database.EnsureCreated(); if (!_ctx.Products.Any()) { //Need to create sample data var filePath = Path.Combine(_env.ContentRootPath, "Data/art.json"); var json = File.ReadAllText(filePath); var products = JsonSerializer.Deserialize <IEnumerable <Product> >(json); _ctx.Products.AddRange(products); var order = new Order() { OrderDate = DateTime.Now, OrderNumber = "10000", Items = new List <OrderItem>() { new OrderItem() { Product = products.First(), Quantity = 5, UnitPrice = products.First().Price } } }; _ctx.Orders.Add(order); _ctx.SaveChanges(); } }
public async Task SeedAsync() { _ctx.Database.EnsureCreated(); StoreUser user = await _userManager.FindByEmailAsync("*****@*****.**"); if (user == null) { user = new StoreUser() { FirstName = "Shawn", LastName = "WIld", Email = "*****@*****.**", UserName = "******" }; var result = await _userManager.CreateAsync(user, "P@ssw0rd!"); if (result != IdentityResult.Success) { throw new InvalidOperationException("Could not create user in seeder"); } } if (!_ctx.Products.Any()) { var filepath = Path.Combine(_hosting.ContentRootPath, "Data/art.json"); var json = File.ReadAllText(filepath); var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json); _ctx.Products.AddRange(products); _ctx.SaveChanges(); } if (!_ctx.Orders.Any()) { _ctx.Orders.Add(new Order() { OrderDate = DateTime.UtcNow, OrderNumber = "12345" }); _ctx.SaveChanges(); var order = _ctx.Orders.Where(o => o.Id == 1).FirstOrDefault(); order.User = user; order.Items = new List <OrderItem>() { new OrderItem() { Product = _ctx.Products.OrderBy(p => p.Id).FirstOrDefault(), Quantity = 5, UnitPrice = _ctx.Products.OrderBy(p => p.Id).FirstOrDefault().Price } }; _ctx.SaveChanges(); } }
public bool SaveAll() { try { _logger.LogInformation("SaveAll was called"); return(_context.SaveChanges() > 0); } catch (Exception ex) { _logger.LogError($"Failed save all: {ex}"); return(false); } }
public void Seed() { _ctx.Database.EnsureCreated(); if (!_ctx.Products.Any()) { //Seed the data //_hosting.ContentRootPath gives root directory var filepath = Path.Combine(_hosting.ContentRootPath, "Data/art.json"); var json = File.ReadAllText(filepath); var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json); _ctx.Products.AddRange(products); var order = new Order() { OrderDate = DateTime.Now, OrderNumber = "12121", Items = new List <OrderItem>() { new OrderItem() { Product = products.FirstOrDefault(), Quantity = 6, UnitPrice = products.FirstOrDefault().Price } } }; _ctx.Orders.AddRange(order); _ctx.SaveChanges(); } }
public void Seed() { _context.Database.EnsureCreated(); if (_context.Products.Any()) { return; } // Need to create sample data var filepath = Path.Combine(_hosting.ContentRootPath, "Data/art.json"); var json = File.ReadAllText(filepath); var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json); _context.Products.AddRange(products); var order = _context.Orders.Where(o => o.Id == 1).FirstOrDefault(); if (order != null) { order.Items = new List <OrderItem>() { new OrderItem() { Product = products.First(), Quantity = 5, UnitPrice = products.First().Price } }; } _context.SaveChanges(); }
//this can be used to seed lookup data public void Seed() { _dutchContext.Database.EnsureCreated(); var filePath = Path.Combine(_hostingEnvironment.ContentRootPath + @"\Data\art.json"); if (!_dutchContext.Products.Any()) { var json = File.ReadAllText(filePath); var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json); _dutchContext.Products.AddRange(products); var order = _dutchContext.Orders.Where(o => o.Id == 2).FirstOrDefault(); if (order != null) { order.Items = new List <OrderItem> { new OrderItem { Product = products.First(), Quantity = 1, UnitPrice = products.First().Price } }; } _dutchContext.SaveChanges(); } }
/// <summary> /// Saves all changes /// </summary> /// <returns></returns> public bool SaveAll() { // SaveChanges returns the number of rows affected, so a // save actually worked if the number of rows affected was // more than 0 return(_ctx.SaveChanges() > 0); }
public void Seed() { _ctx.Database.EnsureCreated(); if (!_ctx.Products.Any()) { //Need to create sample data // useful for when data will be constant for a long time var filepath = Path.Combine(_hosting.ContentRootPath, "Data/bat.json"); var json = File.ReadAllText(filepath); var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json); _ctx.Products.AddRange(products); var order = _ctx.Orders.Where(o => o.Id == 1).FirstOrDefault(); if (order != null) { order.Items = new List <OrderItem>() { new OrderItem() { Product = products.First(), Quantity = 5, UnitPrice = Convert.ToDecimal(products.First().BatPrice) } }; } _ctx.SaveChanges(); } }
public void Seed() { context.Database.EnsureCreated(); if (!context.Products.Any()) { string filePath = Path.Combine(hostingEnvironment.ContentRootPath, "Data/art.json"); string json = File.ReadAllText(filePath); var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json); context.Products.AddRange(products); var order = new Order() { OrderDate = DateTime.Now, OrderNumber = "12345", Items = new List <OrderItem>() { new OrderItem() { Product = products.First(), Quantity = 5, UnitPrice = products.First().Price } } }; context.Orders.Add(order); context.SaveChanges(); } }
public async Task SeedAsync() { //makes sure that the db exists _ctx.Database.EnsureCreated(); //Seed the Main user - find by email StoreUser user = await _userManager.FindByNameAsync("*****@*****.**"); if (user == null) { user = new StoreUser() { FirstName = "Ross", LastName = "Smith", Email = "*****@*****.**", UserName = "******" }; var result = await _userManager.CreateAsync(user, "P4s$w0rd"); if (result != IdentityResult.Success) { throw new InvalidOperationException("Could not create new user in seeder!"); } } IEnumerable <Product> products; if (!_ctx.Products.Any()) { //Need to create sample data var filepath = Path.Combine(_hosting.ContentRootPath, "Data/art.json"); var json = File.ReadAllText(filepath); products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json); _ctx.Products.AddRange(products); } else { products = _ctx.Products; } var order = _ctx.Orders.Where(o => o.Id == 1).FirstOrDefault(); if (order != null) { order.User = user; //this update the order to be owned by the user order.Items = new List <OrderItem>() { new OrderItem() { Product = products.First(), Quantity = 5, UnitPrice = products.First().Price } }; } _ctx.SaveChanges(); }
public void Seed() { //The line of code just below ensures there's a database to begin with, it reduces the chances of querying a database that doesn't exist. //If the database doesn't exist, that method will create it _ctx.Database.EnsureCreated(); //The if statement just below executes if there are no products in the database if (!_ctx.Products.Any()) { // The "Path.Combine" is used to get the root of the environment and add it to the path (that we know) for the "art.json" file //Note that the "art.json" file contains seed data for art products var file = Path.Combine(_hosting.ContentRootPath, "Data/art.json"); var json = File.ReadAllText(file); //The "JsonSerializer" is used to break down the "art.json" file into something that's consistent with what we can easily read then add it into the product model (which has a table in the database) var products = JsonSerializer.Deserialize <IEnumerable <Product> >(json); //AddRange is used because we have a collection of products (which we got from the deserialized "art.json" file). //We specify "Products" below so it knows where the data is going _ctx.Products.AddRange(products); //We're trying to create a new order here var order = new Order() { OrderDate = DateTime.Today, OrderNumber = "10000", //The "Items" is a list because an order can have more than one OrderItem Items = new List <OrderItem>() { new OrderItem() { //This gets the first product from the database and makes it our order Product = products.First(), Quantity = 5, UnitPrice = products.First().Price } } }; // var order = _ctx.Orders.Where(o => o.Id == 1).FirstOrDefault(); // if (order != null) // { // order.Items = new List<OrderItem>() //{ // new OrderItem() // { // Product = products.First(), // Quantity = 5, // UnitPrice = products.First().Price // } //}; _ctx.Orders.Add(order); } //This line of code saves all the changes we've made above _ctx.SaveChanges(); }
public async Task Seed() { //Make sure that before we try to issue any queries, the database actually exists. _ctx.Database.EnsureCreated(); var user = await _userManager.FindByEmailAsync("*****@*****.**"); if (user == null) { user = new StoreUser { FirstName = "Justin", LastName = "Capalbo", UserName = "******", Email = "*****@*****.**" }; var result = await _userManager.CreateAsync(user, "N!tsuj123"); if (result == IdentityResult.Success) { throw new InvalidOperationException("Failed to create the default user"); } } if (!_ctx.Products.Any()) { //Need to create sample data var filepath = Path.Combine(_hosting.ContentRootPath, "Data/art.json"); var json = File.ReadAllText(filepath); //Deserialize the list of art in the json file var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json); //Add products to the context. Not yet ready to save. _ctx.Products.AddRange(products); //Create a sample order. var order = new Order { User = user, OrderDate = DateTime.Now, OrderNumber = "12345", Items = new List <OrderItem> { new OrderItem { Product = products.First(), Quantity = 5, UnitPrice = products.First().Price, } } }; _ctx.Orders.Add(order); //Finally, save the changes. _ctx.SaveChanges(); } }
public async Task SeedAsync() { ctx.Database.EnsureCreated(); //Identity code for a user StoreUser user = await userManager.FindByEmailAsync("*****@*****.**"); if (user == null) { user = new StoreUser() { Firstame = "Waseem", LastName = "Uddin", Email = "*****@*****.**", UserName = "******" }; var result = await userManager.CreateAsync(user, "P@ssw0rd!"); if (result != IdentityResult.Success) { throw new InvalidOperationException("Could not create new user in the seeder"); } } if (!ctx.Products.Any()) { //Need to create sample data var filePath = Path.Combine(hosting.ContentRootPath, "Data/art.json"); var json = File.ReadAllText(filePath); var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json); ctx.Products.AddRange(products); var order = ctx.Orders.Where(o => o.Id == 1).FirstOrDefault(); if (order != null) { //This code will make sure that the user is the authenticated user update the order to be own by // this user order.User = user; order.Items = new List <OrderItem>() { new OrderItem() { Product = products.First(), Quantity = 5, UnitPrice = products.First().Price } }; } ctx.SaveChanges(); } }
// Initialize database with some data public async Task Seed() { // Check if database exists _ctx.Database.EnsureCreated(); // Get or Create user var user = await _userManager.FindByEmailAsync("*****@*****.**"); // Since the FindByEmailAsync method is async the current method "Seed" must be also async and return a Task - only then we can also use await in front of FindByEmailAsync if (user == null) { // If user has not been created yet, create it user = new StoreUser() { FirstName = "traysa", LastName = "admin", UserName = "******", Email = "*****@*****.**" }; var result = await _userManager.CreateAsync(user, "P@ssw0rd!"); // returns an identity result to test againast if (result != IdentityResult.Success) { throw new InvalidOperationException("Failed to create default user"); } } // If there are no products in the database, create some default products // and a sample order if (!_ctx.Products.Any()) { // Need to create sample data var filepath = Path.Combine(_hosting.ContentRootPath, "Data/art.json"); var json = File.ReadAllText(filepath); var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json); _ctx.Products.AddRange(products); var order = new Order() { OrderDate = DateTime.Now, OrderNumber = "12345", User = user, Items = new List <OrderItem>() { new OrderItem() { Product = products.First(), Quantity = 5, UnitPrice = products.First().Price } } }; _ctx.Orders.Add(order); _ctx.SaveChanges(); } }
public async Task SeedAsync() { _ctx.Database.EnsureCreated(); StoreUser user = await _userManager.FindByEmailAsync("*****@*****.**"); if (user == null) { user = new StoreUser() { FirstName = "Kenneth", LastName = "Laforteza", Email = "*****@*****.**", UserName = "******" }; var result = await _userManager.CreateAsync(user, "P@ssw0rd!"); if (result != IdentityResult.Success) { throw new InvalidOperationException("Could not create a new user in seeder"); } } if (!_ctx.Products.Any()) { // Add Products // Need to create sample data string file = Path.Combine(_hosting.ContentRootPath, "Data/art.json"); string jsonData = File.ReadAllText(file); var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(jsonData); _ctx.Products.AddRange(products); // Add Order if (!_ctx.Orders.Any()) { Order order = new Order() { OrderDate = DateTime.Now, OrderNumber = "1N98VJAF8", User = user, Items = new List <OrderItem>() { new OrderItem() { Product = products.First(), Quantity = 5, UnitPrice = products.First().Price } } }; } _ctx.SaveChanges(); } }
public async Task Seed() { _context.Database.EnsureCreated(); var user = await _userManager.FindByEmailAsync("*****@*****.**"); if (user == null) { user = new StoreUser() { FirstName = "Jim", LastName = "Shaw", UserName = "******", Email = "*****@*****.**" }; var result = await _userManager.CreateAsync(user, "P@ssword123!"); if (result != IdentityResult.Success) { throw new InvalidOperationException("Failed to create default user"); } } if (!_context.Products.Any()) { // Need to create sample data. var filePath = Path.Combine(_hosting.ContentRootPath, "Data/art.json"); var json = File.ReadAllText(filePath); var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json); _context.Products.AddRange(products); var order = new Order() { OrderDate = DateTime.Now, OrderNumber = "10001", User = user, Items = new List <OrderItem>() { new OrderItem() { Product = products.First(), Quantity = 5, UnitPrice = products.First().Price } } }; _context.Orders.Add(order); _context.SaveChanges(); } }
public async Task SeedAsync() { _ctx.Database.EnsureCreated(); ///this is how to set up a new user with identity ///above also the dependecy injection StoreUser user = await userManager.FindByEmailAsync("*****@*****.**"); if (user == null) { user = new StoreUser() { FirstName = "Shawn", LastName = "Wildermuth", Email = "*****@*****.**", UserName = "******" }; var result = await userManager.CreateAsync(user, "P@ssw0rd!"); if (result != IdentityResult.Success) { throw new InvalidOperationException("not good user provided"); } } if (!_ctx.Products.Any()) { var filePath = Path.Combine(_hosting.ContentRootPath, "Data/art.json"); var json = File.ReadAllText(filePath); var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json); _ctx.Products.AddRange(products); var ord = new Order { Id = 1, OrderDate = DateTime.Now, OrderNumber = "123", ///Identity piece User = user }; ord.Items = new List <OrderItem>() { new OrderItem() { Product = products.First(), Quantity = 5, UnitPrice = products.First().Price } }; _ctx.SaveChanges(); } }
public async Task SeedAsync() { // Runs previous migrations, like basic Order seeding in SeedData migration. _ctx.Database.EnsureCreated(); // Manages users through the identity system instead of directly through DbContext. StoreUser user = await _userManager.FindByEmailAsync("*****@*****.**"); if (user == null) { user = new StoreUser() { FirstName = "Scott", LastName = "Payne", Email = "*****@*****.**", UserName = "******" }; // Allows configuration of password complexity. Default is decent. var result = await _userManager.CreateAsync(user, "P@ssw0rd!"); if (result != IdentityResult.Success) { throw new InvalidOperationException("Could not create new user in seeder."); } } // Queries DB to check for existing Product records. if (!_ctx.Products.Any()) { // No Products found, needs to seed from sample JSON data. var filepath = Path.Combine(_hosting.ContentRootPath, "Data/art.json"); var json = File.ReadAllText(filepath); var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json); _ctx.Products.AddRange(products); // Orders exist from previous seed done with HasData in DutchContext. var order = _ctx.Orders.Where(o => o.Id == 1).FirstOrDefault(); if (order != null) { order.User = user; order.Items = new List <OrderItem>() { new OrderItem() { Product = products.First(), Quantity = 5, UnitPrice = products.First().Price } }; } _ctx.SaveChanges(); } }
public bool saveAll() { if (context.SaveChanges() > 0) { return(true); } else { return(false); } }
public async Task Seed() { _ctx.Database.EnsureCreated(); var user = await _userManager.FindByEmailAsync("*****@*****.**"); if (user == null) { user = new StoreUser() { FirstName = "Reinaldo", LastName = "Bispo Pereira", UserName = "******", Email = "*****@*****.**", }; var result = await _userManager.CreateAsync(user, "P@ssw0rd!"); if (result != IdentityResult.Success) { throw new InvalidOperationException("Erro ao criar usuário padrão"); } } if (!_ctx.Products.Any()) { //Criar dados de exemplo var filepath = Path.Combine(_hosting.ContentRootPath, "Data/art.json"); var json = File.ReadAllText(filepath); var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json); _ctx.Products.AddRange(products); var order = new Order() { OrderDate = DateTime.Now, OrderNumber = "12345", User = user, Items = new List <OrderItem> { new OrderItem() { Product = products.First(), Quantity = 5, UnitPrice = products.First().Price } } }; _ctx.Orders.Add(order); _ctx.SaveChanges(); } }
public async Task SeedAsync() { _ctx.Database.EnsureCreated(); _ctx.Orders.Add(new Order() { // Id = 1, OrderDate = DateTime.UtcNow, OrderNumber = "12345" }); StoreUser user = await _userManager.FindByEmailAsync("*****@*****.**"); if (user == null) { user = new StoreUser() { FirstName = "Bartek1", LastName = "Czobot", Email = "*****@*****.**", UserName = "******" }; var result = await _userManager.CreateAsync(user, "Kapitan12!"); if (result != IdentityResult.Success) { throw new InvalidOperationException("Could not create new user in seeder"); } } if (_ctx.Products.Any()) { //Need to create sample data var filepath = Path.Combine(_hosting.ContentRootPath, "Data/art.json"); var json = File.ReadAllText(filepath); var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json); _ctx.Products.AddRange(products); var order = _ctx.Orders.Where(o => o.Id == 1).FirstOrDefault(); if (order != null) { order.User = user; order.Items = new List <OrderItem>() { new OrderItem { Product = products.First(), Quantity = 5, UnitPrice = products.First().Price } }; } } _ctx.SaveChanges(); }
public async Task SeedAsync() { //check if database exists... _ctx.Database.EnsureCreated(); StoreUser user = await _userManager.FindByEmailAsync("*****@*****.**"); if (user == null) { user = new StoreUser() { FirstName = "Julio", LastName = "Schurt", Email = "*****@*****.**", UserName = "******" }; var result = await _userManager.CreateAsync(user, "P@ssw0rd!"); if (result != IdentityResult.Success) { throw new InvalidOperationException("COuld not create new user in seeder."); } } if (!_ctx.Products.Any()) { //Need to create sample data var filepath = Path.Combine(_hostingEnvironment.ContentRootPath, "Data/art.json"); var json = File.ReadAllText(filepath); var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json); _ctx.Products.AddRange(products); var order = _ctx.Orders .Where(o => o.Id == 1) .FirstOrDefault(); if (order != null) { order.User = user; order.Items = new List <OrderItem>() { new OrderItem() { Product = products.First(), Quantity = 5, UnitPrice = products.First().Price } }; } _ctx.SaveChanges(); } }
public async Task SeedAsync() { _ctx.Database.EnsureCreated(); StoreUser user = await _userManager.FindByEmailAsync("*****@*****.**"); if (user == null) { user = new StoreUser() { FirstName = "Yan", LastName = "Aung", Email = "*****@*****.**", UserName = "******" }; var result = await _userManager.CreateAsync(user, "P@ssw0rd!"); if (result != IdentityResult.Success) { throw new InvalidOperationException("Could not create new user in seeder"); } } if (!_ctx.Products.Any()) { // Need to create sample data var filePath = Path.Combine(_hosting.ContentRootPath, "Data/art.json"); var json = File.ReadAllText(filePath); var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json); _ctx.Products.AddRange(products); // Assume that migration crated one order with OnModelCreating in DutchContext var order = _ctx.Orders.Where(o => o.Id == 1).FirstOrDefault(); if (order != null) { order.User = user; order.Items = new List <OrderItem>() { new OrderItem() { Product = products.First(), Quantity = 5, UnitPrice = products.First().Price } }; } _ctx.SaveChanges(); } }
public bool SaveAll() { //try //{ return(context.SaveChanges() > 0); //} //catch (Exception ex) //{ //logger.LogError($"Failed to save the changes: {ex}"); //return false; //} }
public bool SaveAll() { try { return(_ctx.SaveChanges() > 0); } catch (Exception ex) { _logger.LogError($"Ocorreu um erro ao executar SaveAll: {ex}"); return(false); } }
public bool SaveAll() { try { return(_context.SaveChanges() > 0); } catch (Exception ex) { _logger.LogError($"Failed save: {ex}"); throw ex; } }
public async Task SeedAsync() { ctx.Database.EnsureCreated(); StoreUser user = await userManager.FindByEmailAsync("*****@*****.**"); if (user == null) { user = new StoreUser() { FistName = "Zeljko", LastName = "Cavic", Email = "*****@*****.**", UserName = "******" }; var results = await userManager.CreateAsync(user, "P@ss0rd!"); if (results != IdentityResult.Success) { throw new InvalidOperationException("Could not create new user in seeder"); } } if (ctx.Products.Any()) { return; } var filePath = Path.Combine(env.ContentRootPath, "Data/art.json"); var json = File.ReadAllText(filePath); var products = JsonSerializer.Deserialize <IEnumerable <Product> >(json); ctx.Products.AddRange(products); var order = ctx.Orders.Where(o => o.Id == 1).FirstOrDefault(); if (order != null) { order.User = user; order.Items = new List <OrderItem>() { new OrderItem() { Product = products.First(), Quantity = 5, UnitPrice = products.First().Price } }; } ; ctx.SaveChanges(); }
public bool SaveAll() { try { return(_ctx.SaveChanges() > 0); } catch (Exception ex) { _logger.LogError($"Failed to save changes: {ex}"); return(false); } }
public async Task SeedAsync() { // check, if database exists _ctx.Database.EnsureCreated(); StoreUser user = await _userManager.FindByEmailAsync("*****@*****.**"); if (user == null) { user = new StoreUser() { FirstName = "T4rn", LastName = "Dev", Email = "*****@*****.**", UserName = "******" }; var result = await _userManager.CreateAsync(user, "123"); if (result != IdentityResult.Success) { throw new Exception($"Could not create new user in seeder: '{JsonConvert.SerializeObject(result)}'"); } } if (!_ctx.Products.Any()) { // need to create sample data var filepath = Path.Combine(_hosting.ContentRootPath, "Data/art.json"); var json = File.ReadAllText(filepath); var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json); _ctx.Products.AddRange(products); var order = _ctx.Orders.Where(o => o.Id == 1).FirstOrDefault(); if (order != null) { order.User = user; order.Items = new List <OrderItem>() { new OrderItem() { Product = products.First(), Quantity = 5, UnitPrice = products.First().Price } }; } _ctx.SaveChanges(); } }
public async Task SeedAsync() { _ctx.Database.EnsureCreated(); StoreUser user = await _userManager.FindByEmailAsync("*****@*****.**"); if (user == null) { user = new StoreUser() { FirstName = "Sreekanth", LastName = "Ugrareddy", Email = "*****@*****.**", UserName = "******", }; String password = "******"; var result = _userManager.CreateAsync(user, password).Result; // await _ctx.SaveChangesAsync(); if (result != IdentityResult.Success) { throw new InvalidOperationException("Could not create the new user!"); } } if (!_ctx.products.Any()) { var filepath = Path.Combine(_hosting.ContentRootPath, "Data/art.json"); var json = File.ReadAllText(filepath); var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json); _ctx.products.AddRange(products); var order = _ctx.Orders.Where(o => o.Id == 1).FirstOrDefault(); if (order != null) { order.User = user; order.Items = new List <OrderItem>() { new OrderItem() { Product = products.First(), Quantity = 5, UnitPrice = products.First().Price } }; } } _ctx.SaveChanges(); }