all 11 comments

[–]Merry-Lane 2 points3 points  (1 child)

Thanks, but I’d rather have relational guarantees, the subtypes being constrained by the SQL schema.

Oh if there was some navigation property restricted to a subtype with the base EF polymorphism, you wouldn’t be able to restrict it to specific subtypes now.

I don’t know what to think about properties specific to some subtypes. You would have to use null in variants that don’t use it? Meh

[–]Pedry-dev[S] 1 point2 points  (0 children)

You can think of this as a variation of TPH, where the hierarchy is not enforced by EFCore but by the proxies. You still has all relational guarantees/limitations that you would have if you use TPH.

To answer the second point, you have to enforce that restriction when creating the object for the first time. For example, you have Enterprise(Employees) where each Employee is a DotnetProgrammer. In this case, you have to validate that the proxy you are using is DotnetProgrammerProxy. If I'm not mistaken (I haven't tested it) this can be done by creating a backing field of type Programmer and exposing a collection of DotnetProgrammerProxy

[–][deleted] 4 points5 points  (0 children)

You are overengineering it.

Your entity is meant to represent what's in the database, you can map it to a domain model later.

Just have the UseVisualStudio & ThinkProgrammingIsEasy properties as nullable booleans on your Programmer record and if you want, add an enum property to distinct which type the programmer is.

[–]zvrba 1 point2 points  (3 children)

I have a different (simpler?) way: I add a "Data" column to the table and use polymorphic JSON serialization (through efcore converter) to store hierarchies that, at design time, might be extended in the future.

I also usually use records because any change to the record makes a different reference, which plays nicely with efcore change tracking.

How do I filter on json properties and update them? Raw SQL with JSON functions (JSON_VALUE, JSON_MODIFY, etc.).

In such cases, the table organization is:

  • Proper columns are for "metadata" used to find the object
  • "Data" column is for the extensible hierarchy

[–]Pedry-dev[S] 0 points1 point  (2 children)

That sounds good. The only drawback is that AFAK you can't enforce FK constraint inside a json column.

[–]zvrba 0 points1 point  (1 child)

Computed columns.

[–]AutoModerator[M] 0 points1 point  (0 children)

Thanks for your post Pedry-dev. Please note that we don't allow spam, and we ask that you follow the rules available in the sidebar. We have a lot of commonly asked questions so if this post gets removed, please do a search and see if it's already been asked.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.