none
How to insert records on a relationship many to many on C# WinForms RRS feed

  • Question

  • Hi Good Morning ...

    I have a one question ... I learn C# with WinForms, I understand the relations with the database, but, on the relationship many to many I don't understand ... 

    I don't have idea how it's do ...

    Do you have an example with explanation the Database operations?

    I'm totally grateful ...

    Regards

    Tuesday, June 18, 2019 11:41 AM

Answers

  • Perhaps using Entity Framework might be an options. For instance, given a person table with a relation to a phone table. Add a new person, add several phone numbers and when the person is inserted it's primary key is used for the foreign key to each phone entry.

    The model created from the database

    Hard coded code sample

    using (var context = new PersonEntities())
    {
        var person = context.People.Add(
            new Person(){FirstName = "Mary", LastName = "Moore"});
    
        person.Phones = new List<Phone>()
        {
            new Phone() { PhoneType = "Cell 1", PhoneNumber = "888-888-8888"},
            new Phone() { PhoneType = "Cell 2", PhoneNumber = "666-666-6666"},
            new Phone() { PhoneType = "Cell 3", PhoneNumber = "444-444-444"}
        };
    
        context.SaveChanges(); 
    
    }
    In a real database setup we would have a person table, a person phone table and a phone table.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by wwwamaurylog Wednesday, June 19, 2019 2:57 PM
    Wednesday, June 19, 2019 2:44 PM
    Moderator

All replies

  • I would suggest reading the first page of the following link which gives a decent explanation 

    http://blog.codeontime.com/2012/04/many-to-many-relationship-in-northwind.html

    See also associative entity (in regards to EmployeeTerrorities on the page above)


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, June 18, 2019 7:54 PM
    Moderator
  • WinForms is a UI library so you are in the wrong forum. The way implement this depends on the database type and data access technology. If you use ADO.Net Data Set or Entity Framework you can ask at https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=adodotnetdataset or https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=adodotnetentityframework. If you want to use a database-specific access technology then ask at the database's developer forum e.g. https://community.oracle.com/community/groundbreakers/database/developer-tools/windows_and_.net/odp.net or https://forums.mysql.com/list.php?38.


    Visual C++ MVP

    Tuesday, June 18, 2019 8:00 PM
    Moderator
  • I'm sorry if my explanation it was not perfect ...

    I'm new for develop systemns ... And I don't have any idea I can do this ...

    My tables are

    ===========

    =   Products    =

    ===========

    Id

    Name

    ===========

    =      Entry      =

    ===========

    Id

    Date

    ====================

    =   Details_Product_Entry   =

    ===================

    Id

    Product_Id

    Entry_Id

    Quantity

    Date

    ====================

    I have my Form on C# with WinForms, and I don't know how obtain the Id register for the Entry table, and use for the Details_Product_Entry ...

    My Logic is the next:

    1. When the click button Save Entry ... Register on Table Entry and the SP on SQL, establish a variable with the Id register with @@Identity

    2. Use a ForEach bucle ... And call class function on my Data Layer with save Details of Entries ...

    I don't know if this correct or if this the best practice ... 

    I'm waiting youre comments ...

    Thanks


    • Edited by wwwamaurylog Wednesday, June 19, 2019 12:57 PM Adding more information
    Wednesday, June 19, 2019 12:50 PM
  • Perhaps using Entity Framework might be an options. For instance, given a person table with a relation to a phone table. Add a new person, add several phone numbers and when the person is inserted it's primary key is used for the foreign key to each phone entry.

    The model created from the database

    Hard coded code sample

    using (var context = new PersonEntities())
    {
        var person = context.People.Add(
            new Person(){FirstName = "Mary", LastName = "Moore"});
    
        person.Phones = new List<Phone>()
        {
            new Phone() { PhoneType = "Cell 1", PhoneNumber = "888-888-8888"},
            new Phone() { PhoneType = "Cell 2", PhoneNumber = "666-666-6666"},
            new Phone() { PhoneType = "Cell 3", PhoneNumber = "444-444-444"}
        };
    
        context.SaveChanges(); 
    
    }
    In a real database setup we would have a person table, a person phone table and a phone table.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by wwwamaurylog Wednesday, June 19, 2019 2:57 PM
    Wednesday, June 19, 2019 2:44 PM
    Moderator