none
Problem with submitting data into tables over WCF (problems in particular on a table with two FK to the same table) RRS feed

  • Question

  • Hello,
    I have the following LINQ entity model:

    a table SortingProjects with the following fields:
    • idSortingProject   (int) PK
    • name   (string)
    a table SortingProcesses with the following fields:
    • idSortingProcess    (int) PK
    • idSortingProject     (int) FK to SortingProjects.idSortingProject
    • name                    (string)
    a table SortingIntermediateConnections with the following fields:
    • idSortingIntermediateConnection     (int)   PK
    • idSourceSP                                    (int)   FK to SortingProcesses.idSortingProcess
    • idDestSP                                       (int)   FK to SortingProcesses.idSortingProcess

    Now, I've a vs2008 solution with:

    - a library DBLib with the dbml for the three tables
    - a WCF Service using the DBLib to connect to database objects
    - a client app (WinForms) referencing the WCF Service and DBLib

    On Client side I do the following:

               
                SortingProject spr = new
     SortingProject();
    
                SortingProcess sp1 = new
     SortingProcess();
                sp1.name = "SP1"
    ;
    
                SortingProcess sp2 = new
     SortingProcess();
                sp2.name = "SP2"
    ;
    
                spr.SortingProcesses.Add(sp1);
                spr.SortingProcesses.Add(sp2);
    
    
                SortingIntermediateConnection sic = new
     SortingIntermediateConnection();
                sic.SortingProcess = sp2;
                sic.SortingProcess1 = sp1;
    
                ServiceReference1.Service1Client sc = new
     linqtest.ServiceReference1.Service1Client();
                sc.SaveSortingProject(spr);
    

    on the WCF Service SaveSortingProject method I do:

                DataClasses1DataContext dc = new
     DataClasses1DataContext();
    
                dc.SortingProjects.InsertOnSubmit(spr);
                
                dc.SubmitChanges();
    

    when I execute this app I get an exception on the SubmitChanges call about a constraint error on the second foreign key from SortingIntermediateConnection.idDestSP to idSortingProcess.

    I've tried to execute the code all on one side (client side just aa a test and WCF side) and everything works fine. If I call dc.GetChangeSet() in the correct case I get 4 Inserts:

    1. SortingProject
    2. SortingProcess
    3. SortingIntermediateConnection
    4. SortingProcess
    when it's not working I get 5:

    1. SortingProject
    2. SortingProcess
    3. SortingIntermediateConnection
    4. SortingProcess
    5. SortingIntermediateConnection

    I'm somehow thinking about something related to serializing/deserializing of the object but googling for a solution hasn't worked till now...
    have you got any advice to make it work?
    thanks
    Roberto
    Friday, March 12, 2010 12:42 PM