locked
issues with Dbcontext.dbset.AddAsync in EF core 3.1 RRS feed

  • Question

  • User2083585850 posted

    Hi,

    I am trying to add a record using DBContext.DBset.AddAsync. This is how my code is:

    await _dbContext.myDbSet.AddAsync(myentity);

    int result = _dbContext.SaveChanges();

    Please note that the datatypes in DBSet, database table and entity passed are in sync.

    But the control is getting lost at _dbContext.SaveChanges() and the data is not getting saved in database. Am I doing something wrong?

    I am using EF core 3.1.2.

    Any help would be appreciated.

    Best Regards,

    Simpy Sinha

    Wednesday, April 8, 2020 8:33 AM

Answers

  • User-854763662 posted

    Hi simpy.sinha ,

    the control is getting lost at _dbContext.SaveChanges()

    What do  you mean ? Is there any error message? You could use try ... catch... block to check the exception during the process of saving data into database and make sure the data that passed to the controller is correct.

    Here is a working demo , you could check the difference bewteen yours

    Model and DbContext

    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public string Address { get; set; }
        public string Phone { get; set; }
    }
    
    public class MVCDbContext : DbContext
    {
         public MVCDbContext(DbContextOptions<MVCDbContext> options) : base(options)
         { }
    
         public DbSet<Employee> Employee { get; set; }
    
    }

    Controller

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> CreateEmployee(Employee employee)
    {
          if (ModelState.IsValid)
          {
                  await _dbContext.Employee.AddAsync(employee);
                  int result = _dbContext.SaveChanges();
                  return RedirectToAction(nameof(Index));
           }
           return View(employee);
    }

    View

    @model MVCDemo3_1.Models.Employee
    @{
        ViewData["Title"] = "CreateEmployee";
    }
    
    <h1>CreateEmployee</h1>
    
    <div class="row">
        <div class="col-md-4">
            <form asp-action="CreateEmployee">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <div class="form-group">
                    <label asp-for="Email" class="control-label"></label>
                    <input asp-for="Email" class="form-control" />
                    <span asp-validation-for="Email" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Name" class="control-label"></label>
                    <input asp-for="Name" class="form-control" />
                    <span asp-validation-for="Name" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Address" class="control-label"></label>
                    <input asp-for="Address" class="form-control" />
                    <span asp-validation-for="Address" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Phone" class="control-label"></label>
                    <input asp-for="Phone" class="form-control" />
                    <span asp-validation-for="Phone" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <input type="submit" value="Create" class="btn btn-primary" />
                </div>
            </form>
        </div>
    </div>

    Result

    If you have any problem , could you share a complete demo that can reproduce the issue ?

    Best Regards,

    Sherry

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 9, 2020 3:05 AM

All replies

  • User475983607 posted

    Adding an entity to the DbContext is illustrated in the following link.

    https://www.learnentityframeworkcore.com/dbcontext/adding-data

    Adding data using DbSet.

    https://forums.asp.net/post/set/1255/2165731/6300144

    Other than following constructs, have you tried running your code through the debugger?  What is the value and type of myentity? 

    Wednesday, April 8, 2020 9:40 AM
  • User-854763662 posted

    Hi simpy.sinha ,

    the control is getting lost at _dbContext.SaveChanges()

    What do  you mean ? Is there any error message? You could use try ... catch... block to check the exception during the process of saving data into database and make sure the data that passed to the controller is correct.

    Here is a working demo , you could check the difference bewteen yours

    Model and DbContext

    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public string Address { get; set; }
        public string Phone { get; set; }
    }
    
    public class MVCDbContext : DbContext
    {
         public MVCDbContext(DbContextOptions<MVCDbContext> options) : base(options)
         { }
    
         public DbSet<Employee> Employee { get; set; }
    
    }

    Controller

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> CreateEmployee(Employee employee)
    {
          if (ModelState.IsValid)
          {
                  await _dbContext.Employee.AddAsync(employee);
                  int result = _dbContext.SaveChanges();
                  return RedirectToAction(nameof(Index));
           }
           return View(employee);
    }

    View

    @model MVCDemo3_1.Models.Employee
    @{
        ViewData["Title"] = "CreateEmployee";
    }
    
    <h1>CreateEmployee</h1>
    
    <div class="row">
        <div class="col-md-4">
            <form asp-action="CreateEmployee">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <div class="form-group">
                    <label asp-for="Email" class="control-label"></label>
                    <input asp-for="Email" class="form-control" />
                    <span asp-validation-for="Email" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Name" class="control-label"></label>
                    <input asp-for="Name" class="form-control" />
                    <span asp-validation-for="Name" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Address" class="control-label"></label>
                    <input asp-for="Address" class="form-control" />
                    <span asp-validation-for="Address" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Phone" class="control-label"></label>
                    <input asp-for="Phone" class="form-control" />
                    <span asp-validation-for="Phone" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <input type="submit" value="Create" class="btn btn-primary" />
                </div>
            </form>
        </div>
    </div>

    Result

    If you have any problem , could you share a complete demo that can reproduce the issue ?

    Best Regards,

    Sherry

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 9, 2020 3:05 AM
  • User2083585850 posted

    Thanks for replying. We got the actual error why is not saving.

    Thanks & Regards,

    Simpy Sinha

    Thursday, April 9, 2020 7:49 AM