-->

ASP.Net Insert to database not updating

2019-09-02 10:49发布

问题:

I'm working with MSSQL Server 2008 and made a database in it. Now i made a ASP.Net (MVC 4) project and connected it to the database. I let visual studio generate the Entity Data Model and everything is working fine so far. Now i have a table for a many to many relation with ofcourse a PK, and two FK. When i manualy insert a row to the database with Microsoft SQL Server Management Studio and i refresh my web page this new row doesn't show up.

I'm 100% sure there is nothing wrong with the C# code (ASP.Net) but after X minutes the row show up :s.

Do i need to update the datasource somewhere or what do i do wrong? (it's my first ASP.Net project :))

Thnx!

Code edit:

private dbEntities db = new dbEntities();

This dbEntities is generated by visual studio from the SQL Server and contains models for all tables.

回答1:

It sounds like your data context is stale (it's retrieving the data from memory and not going to the DB).

See this question or this one for a possible solution.

Edit: Looking at your sample code, I'm going to guess that this has been declared as a page (or class) member. You should wrap this in a using statement so the object context can be disposed:

e.g.

using (var db = new dbEntities())
{
    //perform work
}