Click here to Skip to main content

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrasing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralNo foreign keys on the first date.memberBrady Kelly9 Dec '12 - 2:21 
I wrote this little workaround earlier this year. EF complains about an insert where ParentId is set, but allows me to set it with an update.
// TODO Address this cluster-wtf.
using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }))
{
    if (newProject.ParentId != null)
    {
        // I don't know why I need this, but EF won't insert with a ParentId, but allows me to set it immediately after.
        int parentId = newProject.ParentId.Value;
        newProject.ParentId = null;
        _projectRepository.Insert(newProject);
        newProject.ParentId = parentId;
        _projectRepository.Update(newProject);
    }
    else
    {
        _projectRepository.Insert(newProject);
    }
    scope.Complete();
}

GeneralRe: No foreign keys on the first date.memberKP Lee11 Dec '12 - 8:17 
Assuming this is a SQL Server connection, I don't think you can lay this directly at SQL's door, but you might want to see if triggers are involved and see what they do on insert/update. Maybe the update trigger on the child will insert the parentID in the parent table if it doesn't exist?
 
No, that doesn't make sense, the update should fail with a foreign key constraint failure before it reaches the trigger.
 
So, when it complains, what is its complaint?
GeneralRe: No foreign keys on the first date.memberBrady Kelly11 Dec '12 - 8:34 
Something long winded about referential integrity.
GeneralRe: No foreign keys on the first date.memberKP Lee11 Dec '12 - 11:33 
Brady Kelly wrote:
Something long winded
What else is new with SQL? Big Grin | :-D
GeneralRe: No foreign keys on the first date.protectorPete O'Hanlon11 Dec '12 - 9:10 
Do you have an insert trigger behind this table in the database by any chance? It would seem to be consistent.

*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

"Mind bleach! Send me mind bleach!" - Nagy Vilmos

CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

GeneralRe: No foreign keys on the first date.memberBrady Kelly11 Dec '12 - 16:36 
No. The table is created by EF Code First, nothing but the table itself. Not even indexes but for the PK. I think it's something to do with EF trying to also save another child of the same parent, and clashing somewhere far away.
GeneralRe: No foreign keys on the first date.memberAndrew Rissing13 Dec '12 - 6:42 
As a suggestion, you could debug into the code using the newly open sourced code - http://entityframework.codeplex.com/[^].
GeneralRe: No foreign keys on the first date.memberryan_b200917 Dec '12 - 6:55 
with EF, it helps you out and takes care of all the FK IDs for you Smile | :) You can fill the nested object and let it handle all the FKs (don't even set the ID properties)
ie:
 
parentObject.Project = newProject;
_projectRepositroy.Insert(parentObject);
GeneralRe: No foreign keys on the first date.memberBrady Kelly17 Dec '12 - 7:04 
EF should do that, but somewhere something is amiss - probably my fluent mappings - and I had to resort to the manual update you see in the code excerpt.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 24 May 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid