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. General Programming
  3. .NET (Core and Framework)
  4. Entity data model with Master-detail scenario [modified]

Entity data model with Master-detail scenario [modified]

Scheduled Pinned Locked Moved .NET (Core and Framework)
help
6 Posts 3 Posters 1 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.
  • R Offline
    R Offline
    Ravi Mori
    wrote on last edited by
    #1

    hi, I am using entity data model and i have master-detail scenario.(Patient-PatientAddresses) Everything is going on fine if i am updating any data either in master table or detail table. But when i am going to insert a new detail record while updating master entity, it gives me following error : "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key." I googled so much, but not getting proper idea. Here is my code :

    using (var context = new Entities())
    {
    try
    {
    var record = (from p in context.Patient.Include("PatientAddress")
    where p.PatientId == patient.PatientId
    select p).FirstOrDefault();

                    if (record == null)
                    {
                        context.AddToPatient(patient);
                    }
                    else
                    {
                        foreach (var item in patient.PatientAddress)
                        {
                            if (item.EntityKey == null)
                            {
                                context.AddToPatientAddress(item); // <--Throws Exception here.
                                continue;
                            }
                            context.ApplyPropertyChanges("PatientAddress", item);
                        }
                        context.ApplyPropertyChanges("Patient", patient);
                    }
                    context.SaveChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
    

    Any suggessions is appreciated.

    modified on Wednesday, February 3, 2010 9:55 AM

    D N 2 Replies Last reply
    0
    • R Ravi Mori

      hi, I am using entity data model and i have master-detail scenario.(Patient-PatientAddresses) Everything is going on fine if i am updating any data either in master table or detail table. But when i am going to insert a new detail record while updating master entity, it gives me following error : "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key." I googled so much, but not getting proper idea. Here is my code :

      using (var context = new Entities())
      {
      try
      {
      var record = (from p in context.Patient.Include("PatientAddress")
      where p.PatientId == patient.PatientId
      select p).FirstOrDefault();

                      if (record == null)
                      {
                          context.AddToPatient(patient);
                      }
                      else
                      {
                          foreach (var item in patient.PatientAddress)
                          {
                              if (item.EntityKey == null)
                              {
                                  context.AddToPatientAddress(item); // <--Throws Exception here.
                                  continue;
                              }
                              context.ApplyPropertyChanges("PatientAddress", item);
                          }
                          context.ApplyPropertyChanges("Patient", patient);
                      }
                      context.SaveChanges();
                      return true;
                  }
                  catch
                  {
                      return false;
                  }
              }
      

      Any suggessions is appreciated.

      modified on Wednesday, February 3, 2010 9:55 AM

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Fix your post. The code is unreadable.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008
      But no longer in 2009...

      R 1 Reply Last reply
      0
      • D Dave Kreskowiak

        Fix your post. The code is unreadable.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008
        But no longer in 2009...

        R Offline
        R Offline
        Ravi Mori
        wrote on last edited by
        #3

        Sorry but i can read it from here. Can you tell me which lines of the code you are not able to read?

        D 1 Reply Last reply
        0
        • R Ravi Mori

          hi, I am using entity data model and i have master-detail scenario.(Patient-PatientAddresses) Everything is going on fine if i am updating any data either in master table or detail table. But when i am going to insert a new detail record while updating master entity, it gives me following error : "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key." I googled so much, but not getting proper idea. Here is my code :

          using (var context = new Entities())
          {
          try
          {
          var record = (from p in context.Patient.Include("PatientAddress")
          where p.PatientId == patient.PatientId
          select p).FirstOrDefault();

                          if (record == null)
                          {
                              context.AddToPatient(patient);
                          }
                          else
                          {
                              foreach (var item in patient.PatientAddress)
                              {
                                  if (item.EntityKey == null)
                                  {
                                      context.AddToPatientAddress(item); // <--Throws Exception here.
                                      continue;
                                  }
                                  context.ApplyPropertyChanges("PatientAddress", item);
                              }
                              context.ApplyPropertyChanges("Patient", patient);
                          }
                          context.SaveChanges();
                          return true;
                      }
                      catch
                      {
                          return false;
                      }
                  }
          

          Any suggessions is appreciated.

          modified on Wednesday, February 3, 2010 9:55 AM

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          The exception tells you everything you need to know. If you can't find any useful information then you need to learn how to search better. http://www.bing.com/search?q=An+object+with+the+same+key+already+exists+in+the+ObjectStateManager&src=IE-SearchBox&FORM=IE8SRC[^]


          I know the language. I've read a book. - _Madmatt

          R 1 Reply Last reply
          0
          • R Ravi Mori

            Sorry but i can read it from here. Can you tell me which lines of the code you are not able to read?

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            It's OK now. The first time I looked at it, the entire code snippet was on a single line and you had to scroll left and right to read it all.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007, 2008
            But no longer in 2009...

            1 Reply Last reply
            0
            • N Not Active

              The exception tells you everything you need to know. If you can't find any useful information then you need to learn how to search better. http://www.bing.com/search?q=An+object+with+the+same+key+already+exists+in+the+ObjectStateManager&src=IE-SearchBox&FORM=IE8SRC[^]


              I know the language. I've read a book. - _Madmatt

              R Offline
              R Offline
              Ravi Mori
              wrote on last edited by
              #6

              Ohhh really?????? Thanks a lot.

              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