Base solution for your next web application
Open Closed

STEP BY STEP DEVELOPMENT issue with GetPeople method #5613


User avatar
0
system15 created

Following the asp.net zero documentation step by step guide I have an issue getting the following code snippet to work

public ListResultDto<PersonListDto> GetPeople(GetPeopleInput input)
        {
                var allPersons = _personRepository
                .GetAll()
                .Include(p => p.Phones)
                .OrderBy(p => p.Name)
                .ThenBy(p => p.Surname)
                .ToList();

                return new ListResultDto<PersonListDto>(ObjectMapper.Map<List<PersonListDto>>(allPersons));
        }

I've added this line of code to the CustomDtoMapper.cs

//Phone
            configuration.CreateMap<Phone, PhoneInPersonListDto>();

I get the error message when I try to use the GetPeople method.: "System.InvalidCastException: 'Specified cast is not valid.'" and I don't know how to resolve the issue. Any help would be much appreciated.


8 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    @System15, this might be related to data on the PbPersons table. Could you share the full exception message ?

  • User Avatar
    0
    system15 created
    System.InvalidCastException
      HResult=0x80004002
      Message=Specified cast is not valid.
      Source=Microsoft.EntityFrameworkCore.Relational
      StackTrace:
       at Microsoft.EntityFrameworkCore.Storage.Internal.UntypedRelationalValueBufferFactory.Create(DbDataReader dataReader)
       at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMoveNext(Boolean buffer)
       at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.&lt;&gt;c__DisplayClass12_0`2.<Execute>b__0(DbContext c, TState s)
       at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
       at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, Func`2 operation, Func`2 verifySucceeded, TState state)
       at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
       at Microsoft.EntityFrameworkCore.Query.Internal.QueryBuffer.IncludeCollection(Int32 includeId, INavigation navigation, INavigation inverseNavigation, IEntityType targetEntityType, IClrCollectionAccessor clrCollectionAccessor, IClrPropertySetter inverseClrPropertySetter, Boolean tracking, Object entity, Func`1 relatedEntitiesFactory)
       at Microsoft.EntityFrameworkCore.Query.Internal.IncludeCompiler._Include[TEntity](QueryContext queryContext, TEntity entity, Object[] included, Action`3 fixup)
       at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
       at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__17`2.MoveNext()
       at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at DentsplyDemoNg5.PhoneBook.PersonService.GetPeople(GetPeopleInput input) in C:\Users\x\Documents\DentsplyDemoNg5\src\DentsplyDemoNg5.Application\PhoneBook\PersonService.cs:line 39
       at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.&lt;InvokeActionMethodAsync&gt;d__12.MoveNext()
    
  • User Avatar
    0
    alper created
    Support Team

    Are you using MySQL?

  • User Avatar
    1
    ukocabicak created

    You are probably trying to map multiple entities into a single dto.

    Since PersonListDto and PhoneInPersonListDto properties are not given; I am guessing you are trying to map multiple person entities with multiple phone related entities into multiple PersonListDto which has a single PhoneDto

    Can you be sure about

    public class PersonListDto{ ...  public List<PhoneInPersonListDto> Phones(or whatever) {get; set;} }

    and you have custom mapper configuration for

    configuration.CreateMap<Person, PersonListDto>();

  • User Avatar
    0
    ismcagdas created
    Support Team

    We have a working example, could you compare your code with https://github.com/aspnetzero/aspnet-zero-samples/tree/master/PhoneBook-Angular/aspnet-core/src/Acme.PhoneBookDemo.Application/People ?

    You can also check https://github.com/aspnetzero/aspnet-zero-samples/blob/master/PhoneBook-Angular/aspnet-core/src/Acme.PhoneBookDemo.Application/CustomDtoMapper.cs to see if it is different than your version.

    Don't forget to logint to GitHub with your account before accessing the links.

  • User Avatar
    0
    system15 created

    I'm using SQLServer. I have created a custom mapper but it still doesn't work. I still get the error message Specified cast is not valid. Is there any other ideas that I can try. I am simply following the documentation for the phonebook component.

    Reference: https://www.aspnetzero.com/Documents/Developing-Step-By-Step-Angular Section that I'm getting an issue with: CHANGING GETPEOPLE METHOD.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @system15

    Could you send your project to [email protected] ? We will check it for you.

  • User Avatar
    0
    alper created
    Support Team

    closed due to inactivity.