none
One to many relation Entity Framework 6.0 add operation RRS feed

  • Question

  • Hello

    I'm working on an application for printers auditing. So, when an user choose a printer name, he could see all the users which are related to it. This is my database model : 

    My problem is that I don't understand how I could create a new record with a one to many relationship.

    public void AuditRecord(DateTime date,String user,string[] printer, String pc)
    {
    var USR = new Users ()
    {
        UserName = user,
    };
    
    var PC = new Pc()
    {
        PcName = pc,
    };
    
    var PTR = new Printers();
    List<Printers> ptr = new List<Printers>();
    for (int i = 0;i<printer.Count;i++)
    {
        ptr.Add(new Printers
        {
            PrinterName= printer[i],
        });
    }
    
    var REF = new Reference()
    {
    Date = date,
    };
    
    context.SaveChanges(); }

    I'm lost, and I don't know what I need to do now. A printer is Mapped to an user account (or many). An user use a computer (or many). An user could have mapped many printers. Reference is as Junction table but I don't understand how to synchronise it.


    • Edited by MrFlamby Monday, November 23, 2015 2:00 PM
    • Moved by CoolDadTx Tuesday, November 24, 2015 2:58 PM EF related
    Monday, November 23, 2015 1:52 PM

Answers

  • This solution it's working for insertion.

    ptr.ToList().ForEach(p=>
    {
        var RF = new Reference() {Date=DateTime.Now};
        var USR = new Users() {UserName=user};
        var PC = new Pc() {PcName=pc};
        var PTR = new Printers() {PrinterName = p };
        PTR.Reference.Add(RF);
        USR.Reference.Add(RF);
        PC.Reference.Add(RF);
        context.Pc.Add(PC);
        context.Users.Add(USR);
        context.Printers.Add(PTR);
    });
    context.SavesChanges();

    But I have a problem in reference table. If an user use three printers, I need to insert the Id's for each printer and the same Id's for User and Pc. Like this

    Date     | PrinterId | UserId | PcId
    17/11/15        1         1       1
    17/11/15        2         1       1
    17/11/15        3         1       1

    And I have :

    Date     | PrinterId | UserId | PcId
    17/11/15        1         1       1
    17/11/15        2         2       2
    17/11/15        3         3       3

    Moreover in User and Pc table looks like that :

    | UserId | UserName
         1     John Doe                
         2     John Doe   
         3     John Doe
    There is normally only one record

    Ok, I've found the solution


    Tuesday, November 24, 2015 9:59 AM

All replies

  • This solution it's working for insertion.

    ptr.ToList().ForEach(p=>
    {
        var RF = new Reference() {Date=DateTime.Now};
        var USR = new Users() {UserName=user};
        var PC = new Pc() {PcName=pc};
        var PTR = new Printers() {PrinterName = p };
        PTR.Reference.Add(RF);
        USR.Reference.Add(RF);
        PC.Reference.Add(RF);
        context.Pc.Add(PC);
        context.Users.Add(USR);
        context.Printers.Add(PTR);
    });
    context.SavesChanges();

    But I have a problem in reference table. If an user use three printers, I need to insert the Id's for each printer and the same Id's for User and Pc. Like this

    Date     | PrinterId | UserId | PcId
    17/11/15        1         1       1
    17/11/15        2         1       1
    17/11/15        3         1       1

    And I have :

    Date     | PrinterId | UserId | PcId
    17/11/15        1         1       1
    17/11/15        2         2       2
    17/11/15        3         3       3

    Moreover in User and Pc table looks like that :

    | UserId | UserName
         1     John Doe                
         2     John Doe   
         3     John Doe
    There is normally only one record

    Ok, I've found the solution


    Tuesday, November 24, 2015 9:59 AM
  • Hello,

    Congratulations!

    Can you post the solution here to help other community members who have similar questions?

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, November 25, 2015 8:04 AM
    Moderator