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

Is it possible to have different discriminators for the same entity type? #11890

Closed
search8 opened this issue May 3, 2018 · 5 comments
Closed

Comments

@search8
Copy link

search8 commented May 3, 2018

Hello

I was wondering if it is possible to have different discriminators for the same entity type. What I mean by that is the following:

modelBuilder.Entity<AccountBE>(builder =>
{
   builder.ToTable("Account");
   builder.HasDiscriminator<string>("_accountType")
                 .HasValue<RootAccountBE>(AccountType.Test1.ToString())
                 .HasValue<RootAccountBE>(AccountType.Test2.ToString())
                 .HasValue<ChildAccountBE>(AccountType.Test3.ToString());
}

So I have the two entities (RootAccountBE and ChildAccountBE) which both derive from AccountBE. I have two different Root Account types which have the same properties but are of a different type. So I have specified it as outlined above. It seems though that the discriminator configured last wins.

So when I query an account:

db.Set<T>().FindAsync(id)

For Account Type Test1 I get null, for Account Type Test2 I get a result. If I switch the configuration around (put Type Test2 before Test1) the result is opposite. Is there any way to specify that in a different way or do I have to create an individual class per type?

Thanks,
Serge

@smitpatel
Copy link
Member

I have two different Root Account types which have the same properties but are of a different type.

Can you elaborate what do you mean by different types? Because there is only 1 CLR type RootAccountBE.

@search8
Copy link
Author

search8 commented May 3, 2018

I meant from business side of things they are different but are made up of the same properties that's why I haven't created their own type. What I'm trying to achieve is something like this I guess:

.HasValue<RootAccountBE>(AccountType.Test1.ToString() || AccountType.Test2.ToString())

@smitpatel
Copy link
Member

How do you plan to interpret the correct type? Suppose you are given object of CLR type RootAccountBE how would you find out which type it would map to on business side?

@search8
Copy link
Author

search8 commented May 3, 2018

My RootAccount has an enum property AccountType which is also the discriminator:

public class RootAccountBE : AccountBE
{
        public override AccountType AccountType { get: set: }
}

I mean I can totally create their own types. Maybe that's the better choice in the long run anyways (usually turns out that way). I was just wondering if there is an option to do that or if I'm missing something here.

@ajcvickers
Copy link
Member

@search8 We don't plan to add built in support for this, but #10140 is a more general issue that covers allowing more flexible conditional mapping, which should allow this. So closing as a narrower duplicate of that issue.

@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

3 participants