none
relationships after submitchanges() RRS feed

  • Question

  • I have strange problem:

    I have a person (stakeholder) class and a document class.

    the document class has property name stakeholder that reference the stakeholder which the documnet belongs to.

    I also have stakeholderid the is set to the stakeholder PK column (an int)

    I have the following code :

                Person p = pm.GetById(3);

                Letter newletter = new Letter
                {
                   Created = DateTime.Now,
                    CreatedBy = p.GetName(),
                    StakeHolder = p,
                    StakeHolderId = p.StakeHolderId,
                    Summary = "test"
                };
              im.CreateDocument(newletter); //this calls insertonsubmit and submitchanges

    when I call the submitchanges after insertOnSubmit the DB is updated but after that the stakholderID on the document object is set to 0 (and not 3 as it should be) so if I call again submitchanges if fails because the relationships is not valid (document can not belongs to noone).

     

    why is that? is the mapping incorrect?

     

    Saturday, July 2, 2011 2:45 PM

All replies

  • Hello,

    Thank you for posting.

    Yes, the updated information will not appear until after SubmitChanges has been called. This method is to be inserted, updated or deleted and executes some commands to implement the changes to the databse.

    From your description, we didn't very sure what logic you coded in your project. Please check whether any codes was executed to update stakholderID value or others. Plus, you can also provide more detailed information in your reply.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, July 4, 2011 7:52 AM
  • does newletter  table have primary key?





    Best Regards,
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, July 4, 2011 8:55 AM
  • Hello,

    Thank you for posting.

    Yes, the updated information will not appear until after SubmitChanges has been called. This method is to be inserted, updated or deleted and executes some commands to implement the changes to the databse.

    From your description, we didn't very sure what logic you coded in your project. Please check whether any codes was executed to update stakholderID value or others. Plus, you can also provide more detailed information in your reply.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    the class letter has the following fields:

            public int DocumentId
            {
                get
                {
                    return base.ID;
                }
                set
                {
                    base.ID = value;
                }
            }
            public DocumentType DocumentType { getset; }
            public string Classification { getset; }
            public virtual DateTime Created { getset; }
            public virtual DateTime? LastModified { getset; }
            public virtual string Summary { getset; }
            public virtual string BelongsToSection { getset; }
            public virtual string CreatedBy { getset; }

            public bool IsObsolete { getset; }
            public virtual int StakeHolderId { getset; }
            /// <summary>
            /// Gets/Sets the stakeholder for this document.
            /// EVERY document should be associated with a stakeholder
            /// </summary>
            /// 
            /// <returns>
            /// the stakeholder for this document.
            /// </returns>
            public virtual StakeHolder StakeHolder { getset; }

    the PK is documentId.

    the code above just create a new letter:

     public void CreateLetter()
            {
                serviceFactory = new ClassServiceFactory();
                IPersonManager pm = (IPersonManager)serviceFactory.FindByServiceName(PERSONMAN);
                ILetterManager im = (ILetterManager)serviceFactory.FindByServiceName(LETTERMAN);

                Person p = pm.GetById(3);

                Letter newletter = new Letter
                {
                    BelongsToSection = "none",
                    Classification = "test",
                    Created = DateTime.Now,
                    CreatedBy = p.GetName(),
                    IsObsolete = false,

    //setting the relashinoship of the letter and person (a stakeholder)
                    StakeHolder = p,
                    StakeHolderId = p.StakeHolderId,

                    Summary = "test"
                };
                p.AddDocument(newletter);
                im.CreateDocument(newletter);

                //  Assert.Greater(testUser.Person.StakeHolderId, 0);

            }

     

    the createDocuments is:

            public Letter CreateDocument(Letter newL)
            {
                IServiceFactory serviceFactory = new ClassServiceFactory();
                IDaoFactory df = (IDaoFactory)serviceFactory.FindByServiceName(SERVICENAME);
                ILetterDao ild = df.GetLetterDao();
                newL.LetterId = ild.GetNextId();

                ild.Save(newL);
               
                return newL;
            }

     

    now, before ild.Save(newL) the stakeholderID  of the letter is 3 and after that is zero.

    the PK update to the next one in (dbgenerated)

    Save method is:

     

            public virtual T Save(T entity)
            {
                ITable tab = db.GetTable(entity.GetType().BaseType);
                tab.InsertOnSubmit(entity);
                this.CommitChanges();
                return entity;
            }

     

    why after the save methods the stakeholderID is zet to zero and is changed at al (and not remaons 3)?

    if I call submitchanges after ild.Save(newL) will fail since stakeholderIDis now 0 and not 3 as it should be.

     

     

     

     


    Friday, July 8, 2011 4:13 PM
  • I didn't find some SubmitChanges method after you insertonsubmit method. typically, check this link. http://msdn.microsoft.com/en-us/library/bb763516.aspx If you called SubmitChanges, you can upload along with these code fragement in your post.

    Thanks,

    Esters,


    Just a newbie for everything.
    Monday, July 11, 2011 4:19 PM