Surender Singh Malik
Surender Singh Malik

Reputation: 295

Changes not reflected in Database while using entity framework

I am accessing my database through ADO.NET Entity framework in MVC 3 Application.

I am updating my database through Stored Procedure.

But the changes are not reflected at run time.I mean to say i am able to see the changes only after restarting it.

What is the reason for the problem and How can i avoid it ? I am using Repository pattern So at repository My code look like this

Ther Is One Function Which Save Changes

public void SaveNewAnswer(AnswerViewModel answer,string user) {

        SurveyAdminDBEntities _entities = new SurveyAdminDBEntities();
        _entities.usp_SaveNewAnswer(answer.QuestionId, answer.AnswerName, answer.AnswerText, answer.AnswerOrder, answer.Status, user);
        _entities.SaveChanges();

}

Data Retreival Code

public IEnumerableGetMultipleChoiceQuestions(string questionId) {

        SurveyAdminDBEntities _entities = new SurveyAdminDBEntities();
        _entities.AcceptAllChanges();
        _entities.SaveChanges();
        return _entities.usp_GetMultipleChoiceQuestions(Int32.Parse(questionId));
    }

But Changes are not reflected till the time i don't clode the session of the browser and run it again .

Please help !

Thank You In advance

Upvotes: 1

Views: 5315

Answers (4)

nekno
nekno

Reputation: 19267

Are you calling context.SaveChanges() on your Entities (DbContext/ObjectContext) object? Are you using a transaction that you haven't committed?

If you have an uncommitted transaction in your sproc, you can try creating your own entity transaction and seeing if committing your transaction will commit the nested transaction as well. The problem is that calling SaveChanges() automatically begins and commits a transaction, so this may not be any different than that.

I would also call _entities.AcceptAllChanges() in your save operation.

public void SaveNewAnswer(AnswerViewModel answer,string user) 
{
    SurveyAdminDBEntities _entities = new SurveyAdminDBEntities();
    _entities.Connection.Open();
    System.Data.Common.DbTransaction tran = _entities.Connection.BeginTransaction();        

    try
    {
        _entities.usp_SaveNewAnswer(answer.QuestionId, answer.AnswerName, answer.AnswerText, answer.AnswerOrder, answer.Status, user);
        _entities.SaveChanges(); // automatically uses the open transaction instead of a new one
        tran.Commit();
    }
    catch
    {
        tran.Rollback();
    }
    finally
    {
        if (_entities.Connection.State == System.Data.ConnectionState.Open)
                _entities.Connection.Close();

        _entities.AcceptAllChanges();
    }
}

Upvotes: 2

Adam Tuliper
Adam Tuliper

Reputation: 30152

Add this to your connect string (assuming sql 2005)

transaction binding=Explicit Unbind;

if the data is no longer available after session reset, then the problem is indeed with a transaction, if the data is then available after reset, then your problem is something different and we'll likely need more details.

Upvotes: 0

Ben Finkel
Ben Finkel

Reputation: 4803

When you pull data out of your database into your context that data is kept in memory, separate from the actual database itself.

You will see the changes if you create a new context object instance and load the data from the database with it.

It's good practice to not use the same instance of your context object but create them on an as needed basis for individual transactions with the database. In your case if you're updating via function imports instead of the context.SaveChanges() method then you need to refresh your context with the updated data after you commit those changes.

Upvotes: 0

Brian - TechToolbox
Brian - TechToolbox

Reputation: 29

Is your stored procedure doing an explicit commit? Things run in a database session will be available for that session, but not available to any other session until the action is committed.

Upvotes: 0

Related Questions