-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Relational: TPC inheritance mapping pattern #3170
Comments
Can I make assumption that TPC is now going to be in RC? judged by the following work as expected
|
awesome! |
@wangkanai you are really just getting two separate entity types with an unmapped base type. They each have a separate key space and you won't be able to run a LINQ query on |
If their are in same key space would that work well with sql server auto incremental primary key? |
@wangkanai not with Identity (since that is per table) but you could use a sequence to generate values. What I really meant is that with true TPC you can't have Manager with Id = 1 and an Employee with Id = 1 since then you would have two instances of Person with Id = 1 (and EF would throw if you every tried to have this). In you setup this is possible since they aren't really in an inheritance hierarchy as far as EF is concerned. |
@rowanmiller so if in theory we make a convention to use Id as identity key as |
I'd wanna see TPT before TPC. As @rowanmiller said, @wangkanai example will be used in rare cases when you explicitly wanna keep the entities unmapped or whatever other reason I can't think of now. |
@wangkanai yep that would work. Just to be clear though... with the code you listed back at the start there is no need to ensure that the keys are unique between types since EF is treating them as completely separate types. But for true TPC, yes GUID keys is one option, or a sequence for numeric values, etc. |
@weitzhandler I agree. Our database teams design with high normal forms in mind. TPT is what's required. I am of the opinion that TPC is a weak compromise compared to TPT. I often need to query base classes, and TPT makes this easy. A classic example of this is Martin Fowlers paper on Roles. Our clients will not be moving to EF until TPT is implemented. |
What is the status on this feature? This turns out to be a really important feature in our case. |
The implementation could be simplified if we used a database model #8258 |
+1 For Me - Note to all that you should "thumbs up" the initial post by @rowanmiller to vote up this issue. This and TPT is how DB's are/should be modeled. |
The roadmap lists both TPC and TPT as high priority. Sad to not see either of these tickets in the 2.1 or 3.0 milestone. |
Note: see also #10739 |
Will this ever make it in? 884 days and counting... |
Any news about TPC ? |
This issue is in the Backlog milestone. This means that it is not going to happen for the 2.1 release. We will re-assess the backlog following the 2.1 release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources. |
@rowanmiller We're using TPC with something like #3170 (comment) but we're getting stuck trying to get one to one relationships to work. We get the following exception:
One to many relationships Works correctly, even considering that TPC is not supported in EF Core. Any idea for that? |
@alexdrl If you think you are hitting a bug, then can you please open a new issue including a runnable project/solution/repo or complete code listing that exhibits the behavior? |
I haven't been following closely, but since this closed, is TPC now ready to be played with in the nightly builds? |
@eraffel-MDSol Yes, starting with tomorrow's build |
Hi, `public abstract class AMOS_ADDRESS
.... modelBuilder.Entity<AMOS_ADDRESSCONTACT>().ToTable("ADDRESSCONTACT"); The error I receive when I try to run
is: The corresponding CLR type for entity type 'AMOS_ADDRESS' cannot be instantiated, and there is no derived entity type in the model that corresponds to a concrete CLR type. The same code is perfectly supported by EF6. Regards |
@Luigi6821 Don't call modelBuilder.Entity<AMOS_ADDRESS>().UseTpcMappingStrategy();
modelBuilder.Entity<AMOS_ADDRESSCONTACT>().UseTpcMappingStrategy();
modelBuilder.Entity<ADDRESS>().ToTable("ADDRESS", "AMOS");
modelBuilder.Entity<ADDRESSCONTACT>().ToTable("ADDRESSCONTACT"); Also, you can't yet configure a different column name for one of the tables, that's coming in #19811 |
How are relationships from outside of an TPC hierarchy into any of its entities handled, given that no table is generated for the base/abstract entity? For example, are any of the following supported?
With both TPH and TPT, the following gets generated:
|
@marchy The relationship is handled normally, but no constraint is made in the database. We covered this in the community standup yesterday at around minute 58. |
@ajcvickers With regard to the previous question, how will this work with deletes? Referencing the example given in the video, if I were to delete one of the animals, will the "Human" that references that animal as its favorite be deleted automatically, or would I have to manage preventing "orphan" records like that myself, since there's no FK constraint to enforce that. |
@marchy The database won't perform automatic deletes, but EF will. |
@ajcvickers does this mean that TPC uses Bulk (i.e. set-based) update/delete (without loading data into memory) #795 under the hood or that related entities need to be in the change tracker for cascade to work properly? |
@bachratyg They need to be in the change tracker. |
That means orphans have to be managed manually. |
@bachratyg That depends on your definition of "manually". If data is appropriately loaded, then EF manages them. But if you are looking for a pattern that works well with database constructs like constraints and Identity keys, then TPC is not for you. |
Data being appropriately loaded would mean a read lock on the principal row in all possible transactions modifying the relationship, some clever use of concurrency checks, or a business process that guarantees no concurrent phantom inserts. Either way serious steps should be taken to keep data consistent and that more or less covers my definition of "manually". Don't get me wrong, I don't feel either way about the implementation, I just want to be sure I fully understand the consequences. |
Consider configuring or issuing a warning to help with split identity seeding or shared sequence for the PK
FKs that point to a TPC entity type won't be enforced
Related: #17270 (comment)
The text was updated successfully, but these errors were encountered: