Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not Projecting EF table-per-hierarchy (TPH) inheritance correctly #136

Open
Tinus-H opened this issue Mar 15, 2017 · 0 comments
Open

Not Projecting EF table-per-hierarchy (TPH) inheritance correctly #136

Tinus-H opened this issue Mar 15, 2017 · 0 comments
Assignees

Comments

@Tinus-H
Copy link

Tinus-H commented Mar 15, 2017

Hello Yuriy

I'm having some trouble using the .Project<,> with an EF table-per-hierarchy (TPH) inheritance

I have an Inherited EF Entity with 3 other Entities Inheriting from it.
I'm then trying to Project those Entities to Dtos

Please have a look at the code link below for the full project
https://github.com/Tinus-H/TestDbExample

The problem is as follow

I have these Entities

    public class InheritedSharedEntity : SharedEntity, IInheritedSharedEntity
    {
        public string GroupType { get; set; }
    }

    public class InheritedStringEntity : InheritedSharedEntity, IInheritedStringEntity
    {
        public string StringValue { get; set; }
    }

    public class InheritedIntEntity : InheritedSharedEntity, IInheritedIntEntity
    {
        public int IntValue { get; set; }
    }

    public class InheritedBoolEntity : InheritedSharedEntity, IInheritedBoolEntity
    {
        public bool BoolValue { get; set; }
    }

and I want to Project them to these Dtos

public class InheritedSharedEntityDto : SharedEntityDto, IInheritedSharedEntity
    {
        public string GroupType { get; set; }
    }

    public class InheritedStringEntityDto : InheritedSharedEntityDto, IInheritedStringEntity
    {
        public string StringValue { get; set; }
    }

    public class InheritedIntEntityDto : InheritedSharedEntityDto, IInheritedIntEntity
    {
        public int IntValue { get; set; }
    }

    public class InheritedBoolEntityDto : InheritedSharedEntityDto, IInheritedBoolEntity
    {
        public bool BoolValue { get; set; }
    }

I registered it as follow

        Mapper.Register<InheritedSharedEntity, InheritedSharedEntityDto>()
                .Include<InheritedStringEntity, InheritedStringEntityDto>()
                .Include<InheritedIntEntity, InheritedIntEntityDto>()
                .Include<InheritedBoolEntity, InheritedBoolEntityDto>();

But this does Not Work, it only returns a list of InheritedSharedEntityDto the Base Class

        public IHttpActionResult GetProjectDtos()
        {
            return Ok(_context.InheritedSharedEntities.AsNoTracking()
                .Project<InheritedSharedEntity, InheritedSharedEntityDto>());
        }

But this does Work and return a list of the Derived Classes

        public IHttpActionResult GetMapDtos()
        {
            return Ok(_context.InheritedSharedEntities.AsNoTracking()
                .Map<IEnumerable<InheritedSharedEntity>, IEnumerable<InheritedSharedEntityDto>>());
        }

Because we are using jsonFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.All; we are able to Deserialize the JSON back into the correct Derived Dto Classes client side, we would like to use the .Project<,> because most of our Web API GET calls are OData calls and support paging and the `Map<IEnumerable<>,IEnumerable<>>' queries the whole DB and then only returns the page size results.

Could you please help, I don't know if I'm doing something wrong or if it is even possible.

@anisimovyuriy anisimovyuriy self-assigned this Jul 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants