session.SaveChanges() not working

122 views Asked by At

I'm officially perplexed. I have a Quote object that already exists in the DB, and I'm trying to update it. It never seems to save to the DB after I make my changes and call SaveChanges()

   quote = session.Load<Quote>(QuoteID);
        if (quote == null)
        {
            quote = new Quote();
            session.Store(quote);
        }
        else
        {
            temp.Id = quote.Id;
            temp.Lines = quote.Lines;
        }

        quote = temp;
        //breakpoint here, quote has the correct ID and updated info
        session.SaveChanges();

I refresh the page and it Loads the same quote in Page_Load() and it sets my textboxes back to the old stale data.

   List<Quote> DBquotes = session.Query<Quote>().ToList();
        if (DBquotes.Count() == 0) quote = new Quote();
        else
        {
            //TODO: ability to choose/load quote
            QuoteID = DBquotes[0].Id;
        }

        if (!IsPostBack)
        {
            LoadQuote(QuoteID);
            repHW.DataSource = quote.Lines; 
            repHW.DataBind();
        }

Here's the actual load line in LoadQuote()

    protected void LoadQuote(string ID)
    {
        quote = session.Load<Quote>(QuoteID);
        ...
    }

What am I missing? Why does it not want to save my data? :/

1

There are 1 answers

1
Kent Cooper On BEST ANSWER

Assigning the quote object to temp will not allow you to use session.SaveChanges() without doing another session.Store(quote); So either make the changes on the quote object itself or call session.Store(quote); again.

More Detail: When you load an object from Raven it is actually a proxy class with tracking enabled. If you make changes to this object then you will not have to do anything else other than call session.SaveChanges() However, when you do this quote = temp; you lose the reference to that object and have replaced it with your own object that has no tracking enabled on it.