I have two entities in an EF4 model (using Model First), let's call them EntityA and EntityB. EntityA is marked as abstract, and EntityB inherits from EntityA. They are similar to the following:
public class EntityA
{
public Guid Id;
public string Name;
public string Uri;
}
public class EntityB : EntityA
{
public string AnotherProperty;
}
The generated database tables look as I would expect them, with EntityA as on table, and then another table like:
EntityA_EntityB
Id (PK, FK, uniqueidentifier)
AnotherProperty (varchar)
There is a foreign key constraint on EntityA_EntityB that references EntityA's Id property, no cascades are configured (although I did try changing these myself).
The problem is that when I attempt to do something like:
Context.DeleteObject(EntityA_EntityB);
EF attempts to delete the EntityA_EntityB table record before deleting the EntityA table record, which of course violates the foreign key constraint on EntityA_EntityB table. Using EFProfiler I see the following commands being sent to the database:
delete [dbo].[EntityA_EntityB]
where (([Id] = '5c02899f-09ea-2ed9-d44b-01aef80f6b64' /* @0 */)
followed by
delete [dbo].[EntityA]
where ([Id] = '5c02899f-09ea-2ed9-d44b-01aef80f6b64' /* @0 */)
I'm completely stumped as to how to get around this problem. I would think the EF should know that it needs to delete the base class first, before deleting the inherited class. I know I could do some triggers or other database type solutions, but I'd rather avoid doing that if I can.
All my classes are POCO built using some customized T4 templates. I don't want to paste in a lot of extraneous code, but if you need more information I'll provide what I can.