Cannot implicitly convert type 'Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<T>' to 'T'
Asked Answered
G

1

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?

Geis answered 24/7, 2019 at 14:37 Comment(2)
I don't think you've provided enough code to identify the problem. Show us the relevant code and the entire stacktrace.Modestamodeste
@RobertHarvey As long as the Articles class is just a normal model it should be enough.Masson
M
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;
}
Masson answered 24/7, 2019 at 14:44 Comment(7)
How come the Task didn't show up in the stack trace?Modestamodeste
@RobertHarvey I am pretty sure it is due to the await keyword.Masson
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'Geis
@santoshkumarpatro: Yes, that's what you said in your original post.Modestamodeste
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.Bloodshed
@ChrisPratt Thanks for pointing that out, I updated my answer.Masson
@ChrisPratt and Twenty thanks a lot for your response. It helped me to fix the issue :)Geis

© 2022 - 2024 — McMap. All rights reserved.