none
Why the value changed before I used any DataContext.SubmitChanges()? RRS feed

  • Question

  • When I was debugging, using LinQ to Sql, I found an object updated the database before I used any DataContext.SubmitChanges() after just an evaluation. And It's normal when I run the unit test for DAL.

    My codes like:

    //in DAL.SiteRepository : ISiteRepository

    public Site Get(int ID)
            {
                try
                {
                    return GetAll().FirstOrDefault(item => item.ID == ID);
                }
                catch (Exception error)
                {
                    throw new Exception(error.Message);
                }
            }

            public Table<Site> GetAll()
            {
                try
                {
                    return dataContext.Sites;
                }
                catch (Exception error)
                {
                    throw new Exception(error.Message);
                }
            }

     

    //in BLL.SiteManager : ISiteManager

    public Site GetSite(int ID)
            {
                return siteRepository.Get(ID);
            }

    public void UpdateSite(Site NewItem)
            {
                try
                {
                    if (IfAdmin() || IfMemberAdmin(NewItem.ID, accountManager.GetUser().ID))
                    {
                        var _target = GetSite(Convert.ToInt32(NewItem.ID.ToString()));
                        siteRepository.SaveChanges();
                    }
                }
                catch { }
            }

     

    //in Edit.aspx.cs

            ISiteManager siteManager;
            protected DAL.Site Item;
            protected void Page_Load(object sender, EventArgs e)
            {
                siteManager = ObjectFactory.GetInstance<ISiteManager>();

    //which For<ISiteManager>().HybridHttpOrThreadLocalScoped().Use<SiteManager>() in StructureMap 2.6.1
                Item = siteManager.GetSite(Convert.ToInt32(Request.QueryString.Get("id")));

                if (!IsPostBack)
                {
                    Name_TextBox.Text = Item.Name;
                    Introduction_TextBox.Text = Item.Introduction;
                    Ban_Button.Text = Item.IfBanned ?
                        "OpenSite" : "CloseSite";
                }
            }

            protected void Submit_Button_Click(object sender, EventArgs e)
            {
                if (Name_TextBox.Text != null)
                {

                    //Item still showing the old value here
                    Item.Name = Name_TextBox.Text;

                    //where I stop debugging after multiple times for testing, value already changed in database
                    Item.Introduction = Introduction_TextBox.Text;
                    siteManager.UpdateSite(Item);
                    Response.Redirect("/Admin/Site/");
                }
            }

     

     

    The value was old before 'Item.Name = Name_TextBox.Text', but changed in database after this.

    How could it be before I run to any DataContext.SubmitChanges()?

     

    Tuesday, October 18, 2011 10:02 AM

Answers

  • Extra, I don't know if it's a bug of this website.

    I cannot reply if I click 'Mark As Answer' before I finish the reply. Reply's input box will hides and tells me I'm already editing an reply and should finish that first.

    It became alright after refleshing the page.

    Tuesday, October 18, 2011 10:11 AM

All replies

  • Extra, I don't know if it's a bug of this website.

    I cannot reply if I click 'Mark As Answer' before I finish the reply. Reply's input box will hides and tells me I'm already editing an reply and should finish that first.

    It became alright after refleshing the page.

    Tuesday, October 18, 2011 10:11 AM
  • Hi Luo,

    Thanks for your experience here.

    Have a nice day.


    Alan Chen[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, October 24, 2011 1:17 PM
    Moderator