none
broken relationships in one to many relationships RRS feed

  • Question

  • I have a person class with property docs that contains all the documents of the person.

    in the documnet class I have a property personID (int) and a person propery for quick access to the person.

    when I read back the person (with  dlo.LoadWith<Person>(s => s.Documents);) I get the list of documents with its data (and correct personID) buth the Person propert itself is null.

    I need to manualy set the person property:

                Person person = (from p in db.GetTable<StakeHolder>().OfType<Person>()
                                 where p.StakeHolderId == id
                                 select p).FirstOrDefault();

                foreach (Document d in person.Documents)
                {
                    d.StakeHolder = person;
    // d.StakeHolderId is correct

                }

    why is it needed at all? why LTS doesn't set it by itself?

     


    Thursday, June 30, 2011 4:14 PM

All replies

  • The data is retrieved at the same time the query is executed so, yeah, it is null until you execute the query but you should not worry about it.

    Regards

    Thursday, June 30, 2011 6:23 PM
  • but it doesn't seem to be retrieved in full. After the retretrieve if I call submitChanges I get exception that the relationships are not correct.


    for exmaple this fails:

                var lp = (from s in db.GetTable<StakeHolder>().OfType<Person>()
                          select s).ToList();
                CommitChanges(); <-- does a submitchanges

    but this not:

                var lp = (from s in db.GetTable<StakeHolder>().OfType<Person>()
                          select s).ToList();
              
                foreach (Person person in lp)
                {
                    UpdateRoles(person.Roles, person);
                    UpdateAdminstartedStakeHolder(person);
                 
                }
     CommitChanges();

            private static void UpdateRoles(IEnumerable<Role> roles, StakeHolder person)
            {
                if (roles == nullthrow new ArgumentNullException("roles");
                if (person == nullthrow new ArgumentNullException("person");
                foreach (Role r in roles)
                {
                    r.StakeHolder = person;

                }
            }

            /// <summary>
            /// An helper method to get the administrated StakeHolder  (only first level)
            /// </summary>
            /// <param name="person">the person</param>
            private void UpdateAdminstartedStakeHolder(Person person)
            {
                if (person == nullthrow new ArgumentNullException("person");
                Person administrated = (from p in db.GetTable<StakeHolder>().OfType<Person>()
                                        where p.StakeHolderId == person.AdministratedByStakeHolderId
                                        select p).FirstOrDefault();
                person.AdministratedByStakeHolder = administrated;
            }

     

     

     


    Saturday, July 2, 2011 7:36 AM
  • Typically, we should use InsertOnSubmit to pend a insertation state for instance of datacontext. In addtion, we can also directly call SubmitChanges method like the following thread. http://en.csharp-online.net/LINQ_to_ADO.NET%E2%80%94Add_Modify_Delete_Rows

    Thanks,

    Esters,


    Just a newbie for everything.
    • Marked as answer by Larcolais Gong Friday, July 8, 2011 4:35 PM
    • Unmarked as answer by liorba Friday, July 8, 2011 4:38 PM
    Monday, July 4, 2011 11:12 AM
  • it doesn't answer my question why after reading the data I need to set the relationship manually:

    It doesn't seem to be retrieved in full. After the retretrieve if I call submitChanges I get exception that the relationships are not correct.


    for example this fails:

                var lp = (from s in db.GetTable<StakeHolder>().OfType<Person>()
                          select s).ToList();
                CommitChanges(); <-- does a submitchanges

    but this not:

                var lp = (from s in db.GetTable<StakeHolder>().OfType<Person>()
                          select s).ToList();
              
                foreach (Person person in lp)
                {
                    UpdateRoles(person.Roles, person);
                    UpdateAdminstartedStakeHolder(person);
                 
                }
      CommitChanges();

            private static void UpdateRoles(IEnumerable<Role> roles, StakeHolder person)
            {
                if (roles == nullthrow new ArgumentNullException("roles");
                if (person == nullthrow new ArgumentNullException("person");
                foreach (Role r in roles)
                {
                    r.StakeHolder = person;

                }
            }

            /// <summary>
            /// An helper method to get the administrated StakeHolder  (only first level)
            /// </summary>
            /// <param name="person">the person</param>
            private void UpdateAdminstartedStakeHolder(Person person)
            {
                if (person == nullthrow new ArgumentNullException("person");
                Person administrated = (from p in db.GetTable<StakeHolder>().OfType<Person>()
                                        where p.StakeHolderId == person.AdministratedByStakeHolderId
                                        select p).FirstOrDefault();
                person.AdministratedByStakeHolder = administrated;
            }


    commitChanges is:

            public virtual void CommitChanges()
            {
                try
                {
                    db.SubmitChanges(ConflictMode.ContinueOnConflict);
                }
                catch (ChangeConflictException e)
                {
                    //Log Message to somewhere
                    //e.Message;

                    foreach (ObjectChangeConflict occ in db.ChangeConflicts)
                    {
                        occ.Resolve(RefreshMode.OverwriteCurrentValues);
                    }
                    db.SubmitChanges(ConflictMode.FailOnFirstConflict);
                }
            }
    Friday, July 8, 2011 4:40 PM
  • can you provide exact error information and which line caused your question?

    Thanks,

    Esters,


    Just a newbie for everything.
    Monday, July 11, 2011 3:54 PM