13

I am using ASP.NET core 2.2 for developing web apis. I have the following method in repository class:

public async Task<Articles> AddAsync(Articles article)
{
   return await _context.Articles.AddAsync(article);
}

I am getting the below error:

Cannot implicitly convert type 'Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<Author.Command.Persistence.DBContextAggregate.Articles>' to 'Author.Command.Persistence.DBContextAggregate.Articles'

Here I am trying to use the AddAsync version to save the data.

Can anyone help me to provide their guidance to fix this issue?

2
  • I don't think you've provided enough code to identify the problem. Show us the relevant code and the entire stacktrace. Commented Jul 24, 2019 at 14:39
  • @RobertHarvey As long as the Articles class is just a normal model it should be enough.
    – Twenty
    Commented Jul 24, 2019 at 14:46

1 Answer 1

19

The AddAsync method does not return just the provided type, in your case Articles. It does return Task<EntityEntry>. To fix your issue change your code to the following.

public async Task<Articles> AddAsync(Articles article)
{
   await _context.Articles.AddAsync(article);
   return article;
}

The changes made to the article instance will persist, since EFCore will track the provided Entity. See the MSDN for more information.

What will basically happen now is that your Articles instance is added to the DBSet of the DBContext. If the primary key is generated for you, it will actually set it in the instance you provided the AddAsync method.

EDIT

As Chris Pratt mentioned from the docs

This method is async only to allow special value generators, such as the one used by 'Microsoft.EntityFrameworkCore.Metadata.SqlServerValueGenerationStrategy.SequenceHiLo', to access the database asynchronously. For all other cases the non async method should be used.

So you should instead use the synchronous versions Add instead.

So the code should look something like this.

public Articles Add(Articles article)
{
   _context.Articles.Add(article);
   return article;
}
7
  • How come the Task didn't show up in the stack trace? Commented Jul 24, 2019 at 14:48
  • @RobertHarvey I am pretty sure it is due to the await keyword.
    – Twenty
    Commented Jul 24, 2019 at 14:49
  • This is the error I received : Severity Code Description Project File Line Suppression State Error CS0029 Cannot implicitly convert type 'Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<Author.Command.Persistence.DBContextAggregate.Articles>' to 'Author.Command.Persistence.DBContextAggregate.Articles' Commented Jul 24, 2019 at 14:52
  • @santoshkumarpatro: Yes, that's what you said in your original post. Commented Jul 24, 2019 at 14:52
  • 2
    FWIW, you're not supposed to actually use AddAsync. From the docs: "This method is async only to allow special value generators, such as the one used by 'Microsoft.EntityFrameworkCore.Metadata.SqlServerValueGenerationStrategy.SequenceHiLo', to access the database asynchronously. For all other cases the non async method should be used." Use Add, instead. Commented Jul 24, 2019 at 14:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.