none
Difference between OneToOne & OneToMany RRS feed

  • Question

  • In the dbml, you can create Association (relation) between 2 table, you create either 1To1 or 1ToMany.

    As in northwind database, the relation between Category and Product is 1ToMany, that mean, each category may have ONE or more product. so I can do the following.

    Dim c as new Category
    Dim p as new Product
    c.Product. <-- after the "." I can see .Add() which mean I can insert a child (product) for the parent (category)
    so far so good

    If I change the relation between the 2 table to 1To1, I am no longer able to add a child because when I code c.product. <-- after this there will be no .Add() method to use.

    So, in my sql server tables, when I have 1 to 1 relation, why this does not behave as parent child relation? or it does not consider parent child?

    in order for me to use the .Add() in 1 to 1 relation, I have to change the relation in my dbml to 1 to many. does that effect any performance or difference between 1 to many?

    Thanks.

     

     

     

    Thursday, March 31, 2011 7:41 AM

Answers

  • Because for one element in X there is only one element in Y, that is the whole idea of one to one relationship, adding another element will make it a one to many relationship.

    So Add can only be used in those kind of relationships

    Regards

    • Marked as answer by Samir Ibrahim Thursday, April 7, 2011 6:57 PM
    Thursday, April 7, 2011 6:43 PM
  • You modify the object, you don't add a new one, that way when you submit it the changes are saved.

    Also you can attach and detach objects to a datacontext but be careful.

    My VB skill are not very good so

    Regards

    • Marked as answer by Samir Ibrahim Thursday, April 7, 2011 8:43 PM
    Thursday, April 7, 2011 7:18 PM

All replies

  • Thursday, March 31, 2011 12:06 PM
  • Hi Serguey

    I had read and do to much tests, so far, I still did not get it

    I have 2 tables, ticket and product, in the ticket table there is field id_product , so, for me, ticket is parent, and product is child.

    in the dbml i created 2 association (relation) betweem ticket and product.

                                                                       cardinality      Child Property  
    first assosiation name (ticket_product)          :  OneToMany   True
    second assosiation name (ticket_product1)    :  OneToOne    True

    Now, if I code this

    Dim t as new ticket
    t. <-- after this I have (products and products1)

    if I use products
    t.product.Add( <-- I can write this because after the '.' there is Add

    if I use products1
    t.product1. <-- there is no add

    why is that? because in product the cardinality is onetomany, and in product1 the cardinality is onetoone.

    So my question is, why in OneToOne there is no Add() method?

     

     

     

    Thursday, April 7, 2011 6:37 PM
  • Because for one element in X there is only one element in Y, that is the whole idea of one to one relationship, adding another element will make it a one to many relationship.

    So Add can only be used in those kind of relationships

    Regards

    • Marked as answer by Samir Ibrahim Thursday, April 7, 2011 6:57 PM
    Thursday, April 7, 2011 6:43 PM
  • Thank you. I guess that answer my posted question.

    But not my un-posted question :)

    I have a ticket and ticket_info table, with one to one relation.

    let say ticket (ticket_id autoinc, date,customer_id integer)
    let say ticket_info(autoid autoinc,ticket_id,qty_used)

    dim t as new ticket
    t.date = now
    t.customer_id = 100
    ...

    How to fill the child table ticket_info with data where ticket_info.ticket_id should be the same as ticket.ticket_id with no Add() available ?

    Thanks in advance.



    Thursday, April 7, 2011 7:05 PM
  • You modify the object, you don't add a new one, that way when you submit it the changes are saved.

    Also you can attach and detach objects to a datacontext but be careful.

    My VB skill are not very good so

    Regards

    • Marked as answer by Samir Ibrahim Thursday, April 7, 2011 8:43 PM
    Thursday, April 7, 2011 7:18 PM
  • Very Very cool, 1to1 relation, no add() one submit

    Dim t As new ticket
    t.customer_id = 100
    t.ticket_infos = New ticket_info
    t.ticket_infos.qty_old = 5555
    db.tickets.InsertOnSubmit(t)
    db.SubmitChanges()
    Thursday, April 7, 2011 8:45 PM