0

Sample code:

static async Task Main(string[] args)
{
    using (var context = new DBContext())
        {
            await context.Objects.Add(new Level
            {  
                Id = 1,
                Name = "Name 1"
            });
        }
}

Creates error:

Objects does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'Objects' could be found.

It's TPH database, Objects is class at the top of hierarchy.

I have no idea how to fix it, i'm not sure why it doesn't work here. I have heard it can be issue with using older C# (i don't use 8.0 because i'm unable to upgrade it).

4
  • 4
    DbSet<T>.Add(T) does not return a Task. If you want to persist what you have added to the DbSet<T> then call DbContext.SaveChangesAsync and await the resulting Task that this call returns. Commented Mar 24, 2020 at 16:34
  • i'm not sure why it doesn't work here it wont, there's no task returned as mentioned already. Looking at that code, there's honestly no reason to even try and await it. Commented Mar 24, 2020 at 16:35
  • Missing a closing ) in Add(new Level Commented Mar 24, 2020 at 16:37
  • @Igor It's not the constructor ( that's missing, it's the close for Add. Commented Mar 24, 2020 at 16:38

1 Answer 1

2

Adding doesn't do any heavyweight operations. It simply adds the object to the in-memory context, not the database. Therefore there is no point (or opportunity) to do it asynchronously.

Data is sent to the database only when you do

context.SaveChanges();

so you can do

await context.SaveChangesAsync() 

at that point.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.