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

GetQuery returns BaseClass for EntityRepositoryBase<ConcreteClass, int> #20

Closed
lipchev opened this issue Mar 19, 2019 · 4 comments
Closed

Comments

@lipchev
Copy link

lipchev commented Mar 19, 2019

Greetings,
I have an EF 6 model created using DB first approach. I have further split some tables into abstract + concrete classes using Conditional Mappings (binary discriminators mostly).
I have created my repositories using the concrete classes however GetQuery always returns the base DbSet instead.
I have already added the DbSet< ConcreteClass > to my dbContext (hopefully for documentation purposes only) and it is working as expected.
Now, an easy fix for me would be to have a ConcreteEntityRepositoryBase of sorts that overrides the GetQuery method, returning base.GetQuery().OfType< TEntity >() but wanted to make sure exactly what the intended behavior is for this type of scenario..
Best regards,
Galin LIPCHEV

@GeertvanHorrik
Copy link
Member

Hi Galin,

Thanks for reporting. There are a few things happening in EntityRepositoryBase:

  1. It uses TEntity to determine the entity set name (see https://github.com/WildGums/Orc.EntityFramework/blob/develop/src/Orc.EntityFramework.Shared/Repositories/EntityRepositoryBase.cs#L49)

  2. It uses the `ObjectContext.CreateQuery(_entitySetName) to create the actual query (see https://github.com/WildGums/Orc.EntityFramework/blob/develop/src/Orc.EntityFramework.Shared/Repositories/EntityRepositoryBase.cs#L87)

Are you 100% sure that TEntity is the final class and that you defined it correctly on the DbContext?

@lipchev
Copy link
Author

lipchev commented Mar 19, 2019

It is a somewhat complex inheritance tree and I'm relatively new to EF but the generated classes seem to match my intentions- all but the concrete types are abstract. Note that the Entity Set Name in the designer is read-only, showing not the actual base class (which in my case is SUBSTANCE) but the one above ie MATERIAL (one would think this is because Substance is marked as abstract, but so is MATERIAL..). Here are my two concrete classes (in this branch):

        <EntityType Name="ChemElement" BaseType="CPA.Data.SUBSTANCE" />
        <EntityType Name="ChemCompound" BaseType="CPA.Data.SUBSTANCE" />

        <EntityType Name="SUBSTANCE" BaseType="CPA.Data.ChemEntity" Abstract="true">
        <EntityType Name="ChemEntity" BaseType="CPA.Data.MATERIAL" Abstract="true">
        <EntityType Name="MATERIAL" Abstract="true">

My DbContext defines the sets in partial class like so (probably unnecessary, maybe a way to do this in the designer?) :

        public virtual DbSet<ChemElement> ChemElements { get; set; }
        public virtual DbSet<ChemCompound> ChemCompounds { get; set; }

I've played a bit with the code from GetEntitySetName however none of my concrete types are found in the BaseEntitySets of the container and using their base type as fail-back I think puts us in the "get all scenario".
Furthermore, the initial reason for questioning the intended behavior of the RepositoryBase class was this message from the exception that is thrown when I attempt to do dbContext.GetEntitySet(entityType):

There are no EntitySets defined for the specified entity type 'CPA.Data.ChemElement'. If 'CPA.Data.ChemElement' is a derived type, use the base type instead.

So my thinking was- if there is no explicit OfType anywhere (that I could see)- there is no hope of getting the magical WHERE IsElement=1 clause in..
Anyway- it might just as well be that my model is buggy (as you can see I haven't even gotten to fixing the names yet) and I would hate to waste your time fixing my issues.. so in case what I'm describing is unit-tested somewhere else- maybe ignore this for a while- if the issue is on my side it will likely manifest itself somehow soon.. if not, I'll ping back once I'm confident that the model works 100%.
Anyway here's an extra screenshot from the debugger:
image

@GeertvanHorrik
Copy link
Member

Ok, let's put this on hold so you can figure it out first. If you think you found a bug, please report back (re-open this ticket) with a repro and we will look into it.

@lock
Copy link

lock bot commented Aug 26, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Aug 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants