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. LINQ
  4. Problem when inserting a new Row

Problem when inserting a new Row

Scheduled Pinned Locked Moved LINQ
databasecsharplinqdata-structuresdebugging
2 Posts 2 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.
  • N Offline
    N Offline
    Nitheesh George
    wrote on last edited by
    #1

    Hi, I am developing an application in which we use LINQ to interact with the Database SQLCE. We have a contact table of which ContactId is the primary key.We use compiled query to get the next Maximum value for the primary key. GetAllContactId(objCON).Max() + 1 and LINQ for the compiled query is as follows, public static readonly Func> GetAllContactId = CompiledQuery.Compile> ( (con) > from contact in con.Contact select contact.ContactId ); We are frequently deleting and updating records from this Contact table. The problem comes when deleting the last record from the table and try to insert a new record using with the same primary key an exception displays "Cannot add an entity with a key that is already in use." and the stack trace is as follows at System.Data.Linq.ChangeProcessor.TrackUntrackedObjects(MetaType type, Object item, Dictionary`2 visited) at System.Data.Linq.ChangeProcessor.TrackUntrackedObjects() at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode) at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode) at System.Data.Linq.DataContext.SubmitChanges() . I am not able to figure out the actual problem . Since my table doesnot contain a record with the currenlty generated Primary key value but the link says it exists. thanks Nitheesh

    Jose Jo Martin http://www.simpletools.co.in

    D 1 Reply Last reply
    0
    • N Nitheesh George

      Hi, I am developing an application in which we use LINQ to interact with the Database SQLCE. We have a contact table of which ContactId is the primary key.We use compiled query to get the next Maximum value for the primary key. GetAllContactId(objCON).Max() + 1 and LINQ for the compiled query is as follows, public static readonly Func> GetAllContactId = CompiledQuery.Compile> ( (con) > from contact in con.Contact select contact.ContactId ); We are frequently deleting and updating records from this Contact table. The problem comes when deleting the last record from the table and try to insert a new record using with the same primary key an exception displays "Cannot add an entity with a key that is already in use." and the stack trace is as follows at System.Data.Linq.ChangeProcessor.TrackUntrackedObjects(MetaType type, Object item, Dictionary`2 visited) at System.Data.Linq.ChangeProcessor.TrackUntrackedObjects() at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode) at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode) at System.Data.Linq.DataContext.SubmitChanges() . I am not able to figure out the actual problem . Since my table doesnot contain a record with the currenlty generated Primary key value but the link says it exists. thanks Nitheesh

      Jose Jo Martin http://www.simpletools.co.in

      D Offline
      D Offline
      DoctorMick
      wrote on last edited by
      #2

      Before adding the new record have you commited all previous updates (i.e. the deletion) to the database? If you've only marked it as deleted Linq is very likely to get confused as it will still exist within its internal collections.

      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