I'm sure there is something I'm missing here, and there is a way around this, but for the moment I'm stumped.
I have to kind of pseudo code this, because it's a client database.
I have a "one to zero-to-one" relationship between two entities. The first entity is required for the second entity to exist, via an FK. I am trying to delete the first entity. As I do so, I need to ensure that I delete the dependent entity if it exists.
This is simplified--there are other factors involves (I must save recursively, save within a transactions, can't use cascading deletes, etc. etc.).
I tried this (remember, this is pseudocode):
Entity1.Entity2.Delete();
Entity1.Delete();
Note that the Entity2 table is currently completely empty.
When I save recursively, I get an INSERT failure on the Entity2 table, due to a non-nullable field.
So I tried this:
if (Entity1.Entity2.PK_id > 0)
{
Entity1.Entity2.Delete();
}
Entity1.Delete();
Same error. By checking a field of the entity, I am making a new entity?
Is there a way around this without creating Entity2 independently, fetching it using the Entity1 PK, testing to see if it exists, and then deleting it via Entity1? Doing it this way works, but it seems like there should be a better way.
I hope this question made sense.
Thanks in advance.
Phil