Ejemplo n.º 1
0
        private static void AddSomeDataFromUserInput()
        {
            //before allowing userdata to be implemented into the DB, you would want to add some input validation to ensure that the user is not entering anything malicious.
            using var context = new ChinookContext(options);

            //This will throw an error currently due to the Id not auto-incrementing in the DB itself unless Id is changed to something not currently in the Db.
            Exemployee emp1 = new Exemployee {
                Id = 3, FirstName = "Amanda", LastName = "Ripley", Ssn = "1234447809", DeptId = 2
            };

            context.Add(emp1);//multiple entries can be added to the db by using the AddRange() method and supplying a Collection
            //context.Exemployee.Add(emp1); //This also works, and enforces the type of data being added

            //context.Update(emp1);//Update existing rows
            //context.Exemployee.Update(emp1);

            //create if doesn't exist
            //when we look up an object by primary key specifically, we can use DbSet.Find
            //  it wont fetch from the database again, if the object is already cached in the context. There's no way to combine with .Include()
            var dept = context.Exdepartment
                       .FirstOrDefault(c => c.Id == 10);

            if (dept == null)
            {
                dept = new Exdepartment
                {
                    Id       = 10,
                    Location = "Research Lab"
                };
            }

            context.SaveChanges();
            Console.WriteLine("New Employee has been added...");
        }
Ejemplo n.º 2
0
        static void InsertANewTrack()
        {
            using var context = new ChinookContext(s_dbContextOptions);
            var newTrack = new Track {
                Name = "testTrackName", MediaTypeId = 4, Milliseconds = 1000000, UnitPrice = 1
            };

            context.Add <Track>(newTrack);
            context.SaveChanges();
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Create([Bind("AlbumId,Title,ArtistId")] Album album)
        {
            if (ModelState.IsValid)
            {
                _context.Add(album);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(album));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Create([Bind("Id,Nombres,Apellidos,Cargo,JefeDirecto,Telefono,Email")] Empleado empleado)
        {
            if (ModelState.IsValid)
            {
                _context.Add(empleado);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(empleado));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Create([Bind("ArtistId,Name")] Artist artist)
        {
            if (ModelState.IsValid)
            {
                _context.Add(artist);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(artist));
        }
        public async Task <IActionResult> Create([Bind("GenreId,Name")] Genre genre)
        {
            if (ModelState.IsValid)
            {
                _context.Add(genre);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(genre));
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> Create([Bind("Id,ClienteId,FechaFactura,Total")] Factura factura)
        {
            if (ModelState.IsValid)
            {
                _context.Add(factura);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ClienteId"] = new SelectList(_context.Cliente, "Id", "Id", factura.ClienteId);
            return(View(factura));
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> Create([Bind("Id,Nombre,Apellidos,Telefono,Email,EmpleadoId")] Cliente cliente)
        {
            if (ModelState.IsValid)
            {
                _context.Add(cliente);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["EmpleadoId"] = new SelectList(_context.Empleado, "Id", "Id", cliente.EmpleadoId);
            return(View(cliente));
        }
Ejemplo n.º 9
0
        public async Task <IActionResult> Create([Bind("CustomerId,FirstName,LastName,Company,Address,City,State,Country,PostalCode,Phone,Fax,Email,SupportRepId")] Customer customer)
        {
            if (ModelState.IsValid)
            {
                _context.Add(customer);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["SupportRepId"] = new SelectList(_context.Employee, "EmployeeId", "FirstName", customer.SupportRepId);
            return(View(customer));
        }
        public async Task <IActionResult> Create([Bind("InvoiceId,CustomerId,InvoiceDate,BillingAddress,BillingCity,BillingState,BillingCountry,BillingPostalCode,Total")] Invoice invoice)
        {
            if (ModelState.IsValid)
            {
                _context.Add(invoice);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CustomerId"] = new SelectList(_context.Customer, "CustomerId", "Email", invoice.CustomerId);
            return(View(invoice));
        }
Ejemplo n.º 11
0
        public async Task <IActionResult> Create([Bind("Id,Titulo,ArtistaId")] Album album)
        {
            if (ModelState.IsValid)
            {
                _context.Add(album);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ArtistaId"] = new SelectList(_context.Artista, "Id", "Id", album.ArtistaId);
            return(View(album));
        }
        public async Task <IActionResult> Create([Bind("EmployeeId,LastName,FirstName,Title,ReportsTo,BirthDate,HireDate,Address,City,State,Country,PostalCode,Phone,Fax,Email")] Employee employee)
        {
            if (ModelState.IsValid)
            {
                _context.Add(employee);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ReportsTo"] = new SelectList(_context.Employee, "EmployeeId", "FirstName", employee.ReportsTo);
            return(View(employee));
        }
        public async Task <IActionResult> Create([Bind("InvoiceLineId,InvoiceId,TrackId,UnitPrice,Quantity")] InvoiceLine invoiceLine)
        {
            if (ModelState.IsValid)
            {
                _context.Add(invoiceLine);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["InvoiceId"] = new SelectList(_context.Invoice, "InvoiceId", "InvoiceId", invoiceLine.InvoiceId);
            ViewData["TrackId"]   = new SelectList(_context.Track, "TrackId", "Name", invoiceLine.TrackId);
            return(View(invoiceLine));
        }
Ejemplo n.º 14
0
        public async Task <IActionResult> Create([Bind("Id,Nombre,AlbumId,GeneroId")] Cancion cancion)
        {
            if (ModelState.IsValid)
            {
                _context.Add(cancion);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["AlbumId"]  = new SelectList(_context.Album, "Id", "Id", cancion.AlbumId);
            ViewData["GeneroId"] = new SelectList(_context.Genero, "Id", "Id", cancion.GeneroId);
            return(View(cancion));
        }
Ejemplo n.º 15
0
        public async Task <IActionResult> Create([Bind("Id,FacturaId,CancionId,PrecioUnidad,Cantidad")] DetalleFactura detalleFactura)
        {
            if (ModelState.IsValid)
            {
                _context.Add(detalleFactura);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CancionId"] = new SelectList(_context.Cancion, "Id", "Id", detalleFactura.CancionId);
            ViewData["FacturaId"] = new SelectList(_context.Factura, "Id", "Id", detalleFactura.FacturaId);
            return(View(detalleFactura));
        }
Ejemplo n.º 16
0
        public async Task <IActionResult> Create([Bind("TrackId,Name,AlbumId,MediaTypeId,GenreId,Composer,Milliseconds,Bytes,UnitPrice")] Track track)
        {
            if (ModelState.IsValid)
            {
                _context.Add(track);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["AlbumId"]     = new SelectList(_context.Album, "AlbumId", "Title", track.AlbumId);
            ViewData["GenreId"]     = new SelectList(_context.Genre, "GenreId", "GenreId", track.GenreId);
            ViewData["MediaTypeId"] = new SelectList(_context.MediaType, "MediaTypeId", "MediaTypeId", track.MediaTypeId);
            return(View(track));
        }
Ejemplo n.º 17
0
        public static void InsertANewTrack(ChinookContext ctx)
        {
            Track t = new Track();

            t.Name         = "Test track";
            t.MediaTypeId  = 1;
            t.Composer     = "Weird Al";
            t.Milliseconds = 123456;
            t.UnitPrice    = 0.99M;
            ctx.Add(t);
            ctx.SaveChanges();
            Track n = ctx.Tracks.Where(t => t.Name.Contains("test track")).FirstOrDefault();

            Console.WriteLine($"Here is the new track: {n.Name}");
        }
Ejemplo n.º 18
0
        public static void InsertANewTrack(ChinookContext context)
        {
            Track t = new Track();

            t.TrackId      = context.Tracks.Max(track => track.TrackId) + 1;
            t.Name         = "New_Album_EF";
            t.Composer     = "Andrew";
            t.GenreId      = 3;
            t.UnitPrice    = 3;
            t.Milliseconds = 3000;
            t.MediaTypeId  = 4;
            if (!context.Tracks.Any(track => track.Name == "New_Album_EF"))
            {
                context.Add(t);
                context.SaveChanges();
            }
            printAndrewTracks(context);
        }
Ejemplo n.º 19
0
        public static void InsertANewTrack()
        {
            using var context = new ChinookContext(s_dbContextOptions);
            // figure out what the next track id is
            int nextId = context.Tracks.Count() + 1;

            // create new track
            var newTrack = new Track();

            newTrack.TrackId      = nextId;
            newTrack.Name         = "\"? Two\"";
            newTrack.AlbumId      = 231;
            newTrack.MediaTypeId  = 3;
            newTrack.GenreId      = 19;
            newTrack.Milliseconds = 500;
            newTrack.Bytes        = 1000;
            newTrack.UnitPrice    = 10.99m;

            // add the newTrack to the context
            context.Add(newTrack);

            // save the newTrack to the db
            context.SaveChanges();
        }