CodeSmith Community
Your Code. Your Way. Faster!

DeepSave not working for Many-to-Many Relationships

Latest post 10-10-2006 10:54 PM by msavage123. 5 replies.
  • 10-08-2006 4:39 PM

    DeepSave not working for Many-to-Many Relationships

    I need some assistance deep saving many-to-many relationships. I'm using .netTiers v2.0.0.387.

    I created an extremely simple case where I have three tables: Student (StudentID int PK, Name varchar(50)), Teacher(TeacherID int PK, Name varchar(50)), and StudentTeacher(StudentID int PK, TeacherID int PK).

    I run the following code but the StudentTeacher junction table is never populated (Student and Teacher tables are populated OK):

    // create a new teacher and save
    Teacher
    t = new Teacher();
    t.Name = "Teacher1";
    DataRepository
    .TeacherProvider.Save(t);

    // create a new student
    Student
    s = new Student();
    s.Name = "Student1";

    // add teacher to the student's teacher collection
    s.TeacherCollection_From_StudentTeacher.Add(t);

    // add student/teacher to the junction table
    StudentTeacher
    st = new StudentTeacher();
    st.TeacherID = t.TeacherID;
    s.StudentTeacherCollection.Add(st);

    // deep save the student
    DataRepository
    .StudentProvider.DeepSave(s, DeepSaveType.IncludeChildren, new Type[] { typeof(TList<Teacher>), typeof(TList<StudentTeacher>) });

    Any assistance would be appreciated. Thanks.

    Mike 

     

    • Post Points: 35
  • 10-08-2006 8:14 PM In reply to

    Re: DeepSave not working for Many-to-Many Relationships

    After debugging this, looks like there might be a problem with the DeepSave logic in this scenario, because student provider deep loads the M:M collection first, and then collection is persisting the junction table, and not your student collection. 

    In the meantime, you can get this working by switching the junction table entity to which ever the opposite m:m relationship is, which in this case is your teacher entity instead of your student entity.  Meaning, this would be the table that is opposite the side fo the relationship you are saving from (Student in this case).  Since you are saving from the student provider, the opposite end of the m:m relationships is teacher.

    So it should look like this, and it will work:
     

    ...
    
    // add student/teacher to the junction table
    StudentTeacher st = new StudentTeacher();
    //easier to just add the entities you've already referenced, since in your case, TeacherID doesnt' exist yet, it hasn't been saved. st.TeacherIDSource = t; st.StudentIDSource = s;
    //NOTICE: Adding it to the teacher entity instead;
    t.StudentTeacherCollection.Add(st);
    
    // deep save the student
    DataRepository.StudentProvider.DeepSave(s, DeepSaveType.IncludeChildren, new Type[] { typeof(TList<Teacher>), typeof(TList<StudentTeacher>) });
    

    Robert Hinojosa
    -------------------------------------
    Member of the Codesmith Tools, .netTiers, teams
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 45
  • 10-08-2006 9:31 PM In reply to

    Re: DeepSave not working for Many-to-Many Relationships

    Robert - Thanks for the quick reply. I tried the following based on your reply but got the same result.

                // create a new teacher and save
                Teacher t = new Teacher();
                t.Name = "Teacher1";
                DataRepository.TeacherProvider.Save(t);

                // create a new student
                Student s = new Student();
                s.Name = "Student1";

                StudentTeacher st = new StudentTeacher();
                //easier to just add the entities you've already referenced, since in your case, TeacherID doesnt' exist yet, it hasn't been saved.
                st.TeacherIDSource = t;
                st.StudentIDSource = s;

                //NOTICE: Adding it to the teacher entity instead;

                t.StudentTeacherCollection.Add(st);

                // deep save the student
                DataRepository.StudentProvider.DeepSave(s, DeepSaveType.IncludeChildren, new Type[] { typeof(TList<Teacher>), typeof(TList<StudentTeacher>) });
     

    Inside the DeepSave, CanDeepSave never returns true so no deep save occurs. It seems like this logic is broken. I can upload my source and DDL if that would help you guys.

    Thanks for the help.

    Mike 

    • Post Points: 35
  • 10-08-2006 9:50 PM In reply to

    Re: DeepSave not working for Many-to-Many Relationships

    That's weird, I tried with the exact same setup and it worked.  The only difference is i'm using the latest set of templates.  Go ahead and post the DDL and i'll try it with yours.

    Robert

    On 10/8/06, msavage123 <bounce-msavage123@codesmithsupport.com> wrote:

    Robert - Thanks for the quick reply. I tried the following based on your reply but got the same result.

                // create a new teacher and save
                Teacher t = new Teacher();
                t.Name = "Teacher1";
                DataRepository.TeacherProvider.Save(t);

                // create a new student
                Student s = new Student();
                s.Name = "Student1";

                StudentTeacher st = new StudentTeacher();
                //easier to just add the entities you've already referenced, since in your case, TeacherID doesnt' exist yet, it hasn't been saved.
                st.TeacherIDSource = t;
                st.StudentIDSource = s;

                //NOTICE: Adding it to the teacher entity instead;

                t.StudentTeacherCollection.Add(st);

                // deep save the student
                DataRepository.StudentProvider.DeepSave(s, DeepSaveType.IncludeChildren, new Type[] { typeof(TList<Teacher>), typeof(TList<StudentTeacher>) });
     

    Inside the DeepSave, CanDeepSave never returns true so no deep save occurs. It seems like this logic is broken. I can upload my source and DDL if that would help you guys.

    Thanks for the help.

    Mike 






    Robert Hinojosa
    -------------------------------------
    Member of the Codesmith Tools, .netTiers, teams
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 60
  • 10-08-2006 10:07 PM In reply to

    Re: DeepSave not working for Many-to-Many Relationships

    Here you go:

    CREATE TABLE [dbo].[Student] (
        [StudentID] [int] IDENTITY (1, 1) NOT NULL ,
        [Name] [varchar] (50) NOT NULL
    ) ON [PRIMARY]
    GO

    CREATE TABLE [dbo].[StudentTeacher] (
        [StudentID] [int] NOT NULL ,
        [TeacherID] [int] NOT NULL
    ) ON [PRIMARY]
    GO

    CREATE TABLE [dbo].[Teacher] (
        [TeacherID] [int] IDENTITY (1, 1) NOT NULL ,
        [Name] [varchar] (50) NOT NULL
    ) ON [PRIMARY]
    GO

    ALTER TABLE [dbo].[Student] WITH NOCHECK ADD
        CONSTRAINT [PK_Student] PRIMARY KEY  CLUSTERED
        (
            [StudentID]
        )  ON [PRIMARY]
    GO

    ALTER TABLE [dbo].[StudentTeacher] WITH NOCHECK ADD
        CONSTRAINT [PK_StudentTeacher] PRIMARY KEY  CLUSTERED
        (
            [StudentID],
            [TeacherID]
        )  ON [PRIMARY]
    GO

    ALTER TABLE [dbo].[Teacher] WITH NOCHECK ADD
        CONSTRAINT [PK_Teacher] PRIMARY KEY  CLUSTERED
        (
            [TeacherID]
        )  ON [PRIMARY]
    GO

    ALTER TABLE [dbo].[StudentTeacher] ADD
        CONSTRAINT [FK_StudentTeacher_Student] FOREIGN KEY
        (
            [StudentID]
        ) REFERENCES [dbo].[Student] (
            [StudentID]
        ),
        CONSTRAINT [FK_StudentTeacher_Teacher] FOREIGN KEY
        (
            [TeacherID]
        ) REFERENCES [dbo].[Teacher] (
            [TeacherID]
        )
    GO

     

    • Post Points: 5
  • 10-10-2006 10:54 PM In reply to

    Re: DeepSave not working for Many-to-Many Relationships

    See here:

    http://community.codesmithtools.com/forums/18736/ShowThread.aspx#18736 

    For a proposed solution to this problem.

    Mike 

    • Post Points: 5
Page 1 of 1 (6 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems