E
E
EElliiaann2020-06-20 17:11:15
ASP.NET
EElliiaann, 2020-06-20 17:11:15

ASP.NET Core. Why are records not saved in the database?

My head boils all day from these connections to the database.

Connection:

"ConnectionStrings": {
    "DefaultConnection": "Server=(LocalDb)\\MSSQLLocalDB; Database=EmployeesDatabase; Trusted_Connection=True;"
  },


startup:
string connection = Configuration.GetConnectionString("DefaultConnection");
            services.AddDbContext<AppDbContext>(x => x.UseSqlServer(connection));


Context:
public DbSet<Employee> Employees { get; set; }

        public AppDbContext(DbContextOptions<AppDbContext> options) 
            : base(options) 
        {
            Database.EnsureCreated();
        }


Did Add-Migration init

The database itself was created, but after execution:
public IActionResult AddEmployee(Employee employee)
        {
            db.Employees.Add(employee);
            db.SaveChanges();
            return RedirectToAction("Index");
        }


The entry does not appear on the page. The data from the form is sent to the controller, it is executed, and after it - nothing.
Did I forget to add something?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question