EF Core 1-1 relationship with automatic discriminator
I have a
CommentsThread
entity and multiple ICommentable
entities (Blogpost
, Profile
, Document
, etc.) that all reference it:
now, I would like a way to easily find what the comments thread belongs to. Does it contain comments of a blogpost? User profile? Document?
One way I figured was to reverse the relationship, and instead have a bunch of nullable FKs in the CommentsThread
entity, pointing to all the ICommentable
s. That way, I could do some
but it all seems iffy at best.
Ideally, what I would like instead, was some
properties on the CommentsThread
, with CommentsSource
being an enum.
Problem is, I have nary a clue how to do that. I considered maybe computed columns, but they seemingly can only reference the current entity.12 Replies
I believe I've solved this problem at some point. I'll check it out asap. If I don't in the next 3 hours, dm or ping me.
Worst case scenario you design this database using Oracle DB SQL Developer, deploy it, and then reverse engineering using EF Core
No rush
Also, I'm staying far away from Oracle. This is good ol' code-first Postgres
isn't what you want just a relationship table with an id to CommentsThread and an id to ICommentable? (maybe with some added data for ease of use)
or maybe i'm not understanding the question
Pivot table would not make much sense for a 1-1 relationship
why is it 1-1
No, I want 1-1 where
Foo
, Bar
, Baz
have a foreign key to Slurp
, and Slurp
has a column that has a value of either "foo"
, "bar"
, "baz"
respectivelyah ok so the ICommentable thing can only be 1 type at a time
yep
ICommentable
is really just for convenience and strictness here, on the code side
Doesn't really matter that much for EF code
It's not inheritance with discriminators or what have youok but still what you are asking for ("what the comments thread belongs to") is a collection of elements, because
CommentsThread
belongs to multiple ICommentable
s
am i right on this
iirc postgres has arrays as a type (although managing sync arrays wouldn't be that nice -- and ef has no support for it)It does not belong to multiple
How could it?
Foo
has a foreign key to one and only one Slurp
And each and every Slurp
is only ever referenced by either one single Foo
, one single Bar
, or one single Baz
i got confused by the last part "I have a CommentsThread entity and multiple ICommentable that all reference it:"
now that we've estabilished that the relationship is 1-1 as the title said i can go be low iq somewhere else 😦
Ah, yeah, I can see how that could've been confusing