1

I've a problem that's pertinent to TPH, I believe. This is the scenario: We have one table called Artists, that have some properties about artists (like name, minibio, etc) and a byte field that bits are used to determine the kind of artist is it, with values like:

1 - Singer
2 - Actor
4 - Composer
8 - Musician
16 - Director

Note that the same artist can be just of one kind or pertain to more than one. Let's see an example:

+-------------------------------------------------+
| Table: Artist                                   |
+---------------------------------------+---------+
| Name                                  | Kind    |
+---------------------------------------+---------+
| Al Pacino                             | 2       | (Actor)
+---------------------------------------+---------+
| Mel Gibson                            | 18      | (Actor and Director)
+---------------------------------------+---------+
| Dave Matthews                         | 15      | (Actor, Singer, Composer and Musician)
+---------------------------------------+---------+

Now, I'm able to do TPH using updatable views with stored procedures (for CRUD).
I also know that it's not possible (for me at least!) do this using conditional mapping since I can't use an expression with bitwise OR, like "(Kind | 2) = 2" in the condition.

I've thought, altenativelly, that change the table structure by splitting de field Kind, into something like:

+-----------------------------------------------------------------------------------------+
| Table: Artist                                                                           |
+---------------------------------------+-------+--------+----------+----------+----------+
| Name                                  | Actor | Singer | Composer | Musician | Director |
+---------------------------------------+-------+--------+----------+----------+----------+
| Al Pacino                             |   1   |   0    |    0     |    0     |    0     |
+---------------------------------------+-------+--------+----------+----------+----------+
| Mel Gibson                            |   1   |   0    |    0     |    0     |    1     |
+---------------------------------------+-------+--------+----------+----------+----------+
| Dave Matthews                         |   1   |   1    |    1     |    1     |    0     |
+---------------------------------------+-------+--------+----------+----------+----------+

Could work, but doing this, although I can create TPH using the fields in the conditional mapping, the EF (obviously) gives me an error stating that the derived entities (Actor, Singer, etc) are

being mapped to the same rows in the table Artist.

My question is, are there other ways to do that ?

Thanks in advance.

2 Answers 2

1

Your proposed mapping is neither TPH nor relational. You're also going to have a rather difficult time mapping such a schema to any good OO model. You may also find it hard to query such a DB efficiently. In a TPH scheme, the discriminator field holds a single, scalar value representing one type.This is necessary because any instance can only ever have one type. An instance's type can never change over the course of its lifetime and when you are persisting the instance to a DB then the lifetime of the instance is effectively forever.

So: I think it would be rather difficult to map such a scheme in any way which the EF would understand, and I don't think it would be worth the effort. If this is legacy data, make a view, either in the DB or in the EDMX. If it's a new app, I think you needs to rethink you schema, and come up with a model which fits both an OO and a relational model.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you should give respect to "composition over inheritance" in this case. If a person can be a singer and an actor at the same time, inheritance is clearly the wrong choice for a data structure.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.