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. Linq to Entities: check existence of a record in a database before inserting a new record

Linq to Entities: check existence of a record in a database before inserting a new record

Scheduled Pinned Locked Moved LINQ
questioncsharpdatabaselinqperformance
4 Posts 3 Posters 9 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.
  • I Offline
    I Offline
    ibmkahm
    wrote on last edited by
    #1

    In my web app a user can assign muliple tags to products. There are three tables: products, tags and products_tags to implement a many to many relationship. My question is, how would you implement this with the Entity Framework (LINQ to Entities): "Insert only a new tag in the tags table if it doesnt already exist there, dont add if it exists". So before the insert i have to check first if a tag exists, whats the best way to accomplish this (best performance) ?? (Btw. The Tags table consists of TagID (PK) and TagName) thanks for answers

    N M 2 Replies Last reply
    0
    • I ibmkahm

      In my web app a user can assign muliple tags to products. There are three tables: products, tags and products_tags to implement a many to many relationship. My question is, how would you implement this with the Entity Framework (LINQ to Entities): "Insert only a new tag in the tags table if it doesnt already exist there, dont add if it exists". So before the insert i have to check first if a tag exists, whats the best way to accomplish this (best performance) ?? (Btw. The Tags table consists of TagID (PK) and TagName) thanks for answers

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

      If the appropriate entitykey is set then Linq will handle the insert for you. If you are working with disconnected objects than you must check for, and set the values appropriately. Something like this (not 100% accurate since I'm not on your project)

      Tag t = Tags.FirstOrDefault(e => e.ID == x);
      if(t != null)
      {
      product.Tag = tag;
      }


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

      I 1 Reply Last reply
      0
      • N Not Active

        If the appropriate entitykey is set then Linq will handle the insert for you. If you are working with disconnected objects than you must check for, and set the values appropriately. Something like this (not 100% accurate since I'm not on your project)

        Tag t = Tags.FirstOrDefault(e => e.ID == x);
        if(t != null)
        {
        product.Tag = tag;
        }


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

        I Offline
        I Offline
        ibmkahm
        wrote on last edited by
        #3

        thanks a lot for the answer but i'm still stuck. The entitykey is set (TagID)in my Entity Model, just like the Primarykey in the Database. Besides TagID (PK) the tag table has only one more column: tagName. So when i insert a tag that already exists (for exc. tag.tagName = tag1) the database just inserts another tag1. I tried to add a unique contraint to the tagName column, then i get an exception when trying to insert another tag1. I'm not sure what you mean with "disconnected objects", so i will just describe my procect a little more: in my web app there is a form where users can submit products (and add for exc. tags to it), when the user clicks a submit button the product information are saved (with the help of the Entity Framework) in the database. "Tag t = Tags.FirstOrDefault(e =>; e.ID == x)" is to check if there is already a certain record (tag) in the database ?

        1 Reply Last reply
        0
        • I ibmkahm

          In my web app a user can assign muliple tags to products. There are three tables: products, tags and products_tags to implement a many to many relationship. My question is, how would you implement this with the Entity Framework (LINQ to Entities): "Insert only a new tag in the tags table if it doesnt already exist there, dont add if it exists". So before the insert i have to check first if a tag exists, whats the best way to accomplish this (best performance) ?? (Btw. The Tags table consists of TagID (PK) and TagName) thanks for answers

          M Offline
          M Offline
          musefan
          wrote on last edited by
          #4

          I think the other answer is slightly wrong for your needs, try something like...

          if(DB.products_tags.Count(t => t.Product.ID == productID && t.Tag.TagName == tagName) == 0)
          {
          //No conflicting tag found so can insert the new tag
          }

          Life goes very fast. Tomorrow, today is already yesterday.

          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