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. Linq2Sql: Adding an object with childs

Linq2Sql: Adding an object with childs

Scheduled Pinned Locked Moved LINQ
helpcsharpdatabaselinq
1 Posts 1 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.
  • Q Offline
    Q Offline
    Quake2Player
    wrote on last edited by
    #1

    Lets say my Linq 2 Sql model is like this: Class Movement { Products, ... } Class Product { ... } The problem is that if I create a Movement and populate all of its properties, like:

    Movement m = new Movement()
    m.SomeProperty = ...
    m.SomeProperty = ...
    m.Products.Add ( someExistingProduct )
    m.Products.Add ( someExistingProduct )
    m.Products.Add ( someExistingProduct )

    movementsRepository.Add ( m )
    movementsRepository.Save()

    I get an error "An attempt has been made to Attach or Add an entity that is not new" The only why i'm getting to achieve the add is like this:

    Movement m = new Movement()
    m.SomeProperty = ...
    m.SomeProperty = ...

    movementsRepository.Add ( m )
    movementsRepository.Save()

    someExistingProduct = productsRepository.GetById ( ... )
    someExistingProduct.Movement = m
    productsRepository.Save()

    someExistingProduct = productsRepository.GetById ( ... )
    someExistingProduct.Movement = m
    productsRepository.Save()

    etc..

    The problem with this second approach is that if there is a problem with one of the products (which are retrieved by an id specified on a form, etc.) I would need to delete the Movement and etc. I dont find it a very elegant solution. Help please, Thanks

    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