Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. No foreign keys on the first date.

No foreign keys on the first date.

Scheduled Pinned Locked Moved The Weird and The Wonderful
announcement
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Brady Kelly
    wrote on last edited by
    #1

    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();
    }

    K P A R 4 Replies Last reply
    0
    • B Brady Kelly

      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();
      }

      K Offline
      K Offline
      KP Lee
      wrote on last edited by
      #2

      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?

      B 1 Reply Last reply
      0
      • K KP Lee

        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?

        B Offline
        B Offline
        Brady Kelly
        wrote on last edited by
        #3

        Something long winded about referential integrity.

        K 1 Reply Last reply
        0
        • B Brady Kelly

          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();
          }

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          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

          B 1 Reply Last reply
          0
          • B Brady Kelly

            Something long winded about referential integrity.

            K Offline
            K Offline
            KP Lee
            wrote on last edited by
            #5

            Brady Kelly wrote:

            Something long winded

            What else is new with SQL? :-D

            1 Reply Last reply
            0
            • P Pete OHanlon

              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

              B Offline
              B Offline
              Brady Kelly
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • B Brady Kelly

                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();
                }

                A Offline
                A Offline
                Andrew Rissing
                wrote on last edited by
                #7

                As a suggestion, you could debug into the code using the newly open sourced code - http://entityframework.codeplex.com/[^].

                1 Reply Last reply
                0
                • B Brady Kelly

                  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();
                  }

                  R Offline
                  R Offline
                  ryan_b2009
                  wrote on last edited by
                  #8

                  with EF, it helps you out and takes care of all the FK IDs for you :) 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);

                  B 1 Reply Last reply
                  0
                  • R ryan_b2009

                    with EF, it helps you out and takes care of all the FK IDs for you :) 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);

                    B Offline
                    B Offline
                    Brady Kelly
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups