CONTROLLER using Kolokwium.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using System.Threading.Tasks; public class StudentController:Controller { private readonly MyDbContext _context; public StudentController(MyDbContext context) { _context = context; } public async Task Index() { var dane = await _context.Studenci.ToListAsync(); return View(dane); } public IActionResult Add() { return View(); } public IActionResult DodajStudenta(Student student) { _context.Studenci.Add(student); _context.SaveChanges(); return Redirect("Index"); } } ------------------------------------------------------------------------------------------------------ INDEX @using Kolokwium.Models; @model ICollection Dodaj nowego studenta @foreach(Student student in Model) {

@student.Name

@student.LastName

@student.NrInex

} ------------------------------------------------------------------------------------------------------ PROGRAM using Microsoft.EntityFrameworkCore; using Kolokwium.Models; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllersWithViews(); ________________ var ConnectionString = builder.Configuration.GetConnectionString("DefaultConnection"); builder.Services.AddDbContext(options => options.UseSqlServer(ConnectionString)); ________________ var app = builder.Build(); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. //app.UseHsts(); } //app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); app.Run(); --------------------------------------------------------------------------------------------------------- MYDBCONTEXT using Microsoft.EntityFrameworkCore; namespace Kolokwium.Models { public class MyDbContext:DbContext { public MyDbContext(DbContextOptions options):base(options) { } public DbSet Studenci {get; set;} } } -------------------------------------------------------------------------------------------------------- ADD @using Kolokwium.Models; @model Student;
-------------------------------------------------------------------------------------------------------- dotnet add package Microsoft.EntityFrameworkCore.SqlServer dotnet add package Microsoft.EntityFrameworkCore.Sqlite dotnet add package Microsoft.EntityFrameworkCore dotnet add package Microsoft.EntityFrameworkCore.Design dotnet add package Microsoft.EntityFrameworkCore.Tools dotnet tool install --global dotnet-ef dotnet ef database update