public static void AddShelf(string name) { using (ShelvesContext context = new ShelvesContext()) { if (context.Shelves.Where(x => x.Name.ToUpper() == name.Trim().ToUpper()).Count() != 0) { throw new Exception("That shelf already exists."); } // context.Shelves.Add(new Shelves() { Name = name.Trim() }); // context.Add(context.ShelfMaterials.Include(b => b.ID)); var s1 = context.ShelfMaterials.Include(b => b.Shelves).First(); var s2 = new Shelves { Name = name.Trim() }; s1.Shelves.Add(s2); context.SaveChanges(); } }
/* * A parameter for “shelfMaterial” * Ensure the material exists in the “ShelfMaterial” table * if material does not exist, let the user know and exit. * “shelfMaterial” parameter should be case insensitive. * “shelfMaterial” parameter should be trimmed. * When you add the new shelf, ensure the foreign key for “ShelfMaterial” is populated correctly. */ static void Main(string[] args) { using (ShelvesContext context = new ShelvesContext()) { Console.WriteLine("Please enter name a shelf: "); context.CreateShelf(Console.ReadLine().Trim().ToLower()); } }
public static void AddMaterial(string shelfMaterial) { using (ShelvesContext context = new ShelvesContext()) { string materialName = shelfMaterial; if (context.ShelfMaterials.Where(x => x.MaterialName == materialName.Trim()).Count() != 0) { throw new Exception("That material already exists."); } context.ShelfMaterials.Add(new ShelfMaterials { MaterialName = materialName.Trim() }); context.SaveChanges(); Console.WriteLine($"Self MaterialName is {materialName}"); } }
public ShelfService(ShelvesContext context) => _shelves = context.Shelves;