My code is as below.
public void CreateNewAuthor(List<Author> newAuthor)
{
publishContext.AddToAuthors(newAuthor);
}
I know this will result in error as AddToAuthors(Author newAuthor) accepts entity object as parameter while I am passing a List<>. So how this should be handled? How to cast a List<> to entity object before AddToAuthors()?
You’re accepting multiple authors – but you’re trying to call something which takes a single author. Are you expecting multiple values within your list, or just a single one?
It sounds like you might just want to loop:
… or it’s entirely possible that the context already provides a way of adding multiple authors at a time. (I’m not an EF person, so I don’t know for sure.)
The important thing is that you understand the possibilities here – the list could contain no authors, one author or multiple authors. Are all of those valid in your code? How do you want to handle each of those situations?