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

TPH, entity discriminator with multiple values. #20512

Closed
alessandroros opened this issue Apr 3, 2020 · 2 comments
Closed

TPH, entity discriminator with multiple values. #20512

alessandroros opened this issue Apr 3, 2020 · 2 comments

Comments

@alessandroros
Copy link

alessandroros commented Apr 3, 2020

I have a legacy DB with some limited possibilities to change data structure. Suppose that you have a field called "Origin" in this example, which discriminates entities in different 2 child entities. An entity is an ItemA if it has 'X' or ' Y' in the Origin field.

Is possible to map child entities to have more than one value to discriminate the correct entity?
What I'm trying to configure is the following mapping:

     protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Item>()
                           .HasDiscriminator<string>("origin")
                           .HasValue<ItemA>("X")
                           .HasValue<ItemA>("Y")
                           .HasValue<ItemB>("Z");
        }        
    }

    public abstract class Item
    {
        public int ItemId { get; set; }
        public string Name { get; set; }
    }

    public class ItemA : Item
    {
        public string PropA { get; set; }
    }

    public class ItemB : Item
    {
        public string PropB { get; set; }
    }
}

Currently it takes the last "hasValue" it sees (in this case for example 'Y' will be used as where clause) for searching ItemA entity, so it seems is not working properly. Do you have any tips or future feature which could be helpful in order to solve this scenario?

EF Core version: 3.1.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET Core 3.0
Operating system: Windows 10
IDE: Rider

@alessandroros
Copy link
Author

I understood now that there is a problem with this configuration. Once it has to save the entity it doesn't know what to save since it has two possible values ('X' or 'Y'). Close this request.
Thanks.

@ajcvickers
Copy link
Member

@elciopa See #10140 for discussion of more flexible discriminator mapping.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants