Reputation: 10917
I have two tables FilesystemEntries and CacheEntries where there is an association of 0..1 CacheEntry per FilesystemEntry (that is a FilesystemEntry can have a null CacheEntry, but not vice-versa). Initially, the CacheEntry for all FilesystemEntries is null. I'm having trouble changing this (adding a new CacheEntry). The code (truncated) looks like this:
FilesystemEntry filesystemEntry; // Already exists in database
CacheEntry cacheEntry; // A new one is created
// ...
filesystemEntry.CacheEntry = cacheEntry; // Was null before (verified in debugger)
cacheEntry.FilesystemEntry = filesystemEntry;
_db.AddToCacheEntries(cacheEntry);
However, I'm recieving the following error:
System.Data.UpdateException: A relationship is being added or deleted from an
AssociationSet 'FK_CacheEntries_0'. With cardinality constraints, a corresponding
'CacheEntries' must also be added or deleted.
Any entity framework wizards know what's going on?
Similarly, is there any way to allow the database to handle the "ON DELETE CASCADE" (I'm using sqlite, so it'll be via a trigger)? This would be a lot more convenient and future-proof than specifying all the deletes in the DAL.
Upvotes: 0
Views: 1367
Reputation: 7845
I may be wrong, but unless I'm misunderstanding your entity relationships, you might try it without the
cacheEntry.FilesystemEntry = filesystemEntry;
line. This should be implicit by the fact that you set
filesystemEntry.CacheEntry = cacheEntry;
Also I've had good luck using the following type of assignment when dealing with navigation properties:
cacheEntry.FilesystemEntryReference.Value = filesystemEntry;
Upvotes: 0
Reputation: 20914
Not sure if this is the problem.
But I'd try doing this:
FilesystemEntry filesystemEntry; // Already exists in database
CacheEntry cacheEntry; // A new one is created
filesystemEntry.CacheEntry = cacheEntry;
// redundant cacheEntry.FilesystemEntry = filesystemEntry;
// redundant _db.AddToCacheEntries(cacheEntry);
_db.SaveChanges();
The first redundant operation might be causing the problem.
As for cascade delete take a look at this Tip for an explanation of how Cascade delete really works in EF.
Alex
Upvotes: 2