AddAsync() method is missing

.net c# entity-framework-core

Question

I am using Entity Framework Core. I want to create an async method which will create new user in my database, i have included all the libraries I need, but some methods that are supposed to work with database are missing, I have almost every async method but i am missing AddAsync and RemoveAsync. When I type AddAsync manually I get the following error message: "Error CS1061 'DbSet' does not contain a definition for 'AddAsync' and no accessible extension method 'AddAsync' accepting a first argument of type 'DbSet' could be found (are you missing a using directive or an assembly reference?)"

The class where the method is created has following code and libraries included:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.Data;
using Microsoft.EntityFrameworkCore;
namespace ClassLibrary
{
    public class Class1 : Interface1
    {
        public async Task AddKorisnik(Korisnici k)
        {
            using (ExtentEntities context = new ExtentEntities())
            {
                context.Korisnici.AddAsync();
                await context.SaveChangesAsync();
            }
        }
    }
}

The class where the DbContext is used is following:

namespace ClassLibrary
{
    using System;
    using System.Linq;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class ExtentEntities : DbContext
    {
        public ExtentEntities()
            : base("name=ExtentEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<Korisnici> Korisnici { get; set; }
    }
}
1
0
4/30/2019 2:11:17 PM

Accepted Answer

There is nothing asynchronous with adding an object to an in-memory DbSet<T>. You should use the synchronous Add method to do this.

SaveChangesAsync() is the method that actually connects asynchronously to the underlying database.

3
4/30/2019 2:17:58 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow