Telefisch
Telefisch

Reputation: 315

How to refresh values from database asp.net

I'm fighting with my ASP.NET MVC site, when reloading records from database.

In my scenario, I have a site to insert some data. Additionally, I have a graphic with some svg, where these data are shown also.

If I now change any of the data and then reopen the graphic, only the old values are shown, even on reload of the page.

The data is loaded into a viewBag model, within the action method that opens the site, but the method, that loads the data returns these old values.

If I load the data after restarting the site (debug stop/start) everything is as it should.

Here, the data should be loaded, for the viewBag model:

EditorViewModel model = new EditorViewModel
        {
            Project = projectModel,
            ProjectSymbols = SymbolDataProvider.GetDxProjectSymbols(projectModel.Id),
            TemplateSymbols = SymbolDataProvider.GetDxTemplateSymbols(),
            UsedProjectSymbols = SymbolDataProvider.GetProjectSymbols(projectModel.Id),
            ProjectItems = SymbolDataProvider.GetProjectFunctionGroups(Id)
        };
return View(model);

This is where the data are loaded from database:

    public static IList<ProjectSymbols> GetProjectSymbols(int projectId)
    {
        string AKZ = "";
        string OKZ = "";

        IEnumerable<Symbol> symbols = db.Symbols.ToList();
        IList<SubFunction> subFunctions = db.SubFunctions.Where(x => x.Function.FunctionGroup.ProjectId == projectId).ToList();

        List<ProjectSymbols> projectSymbols = new List<ProjectSymbols>();

        foreach (SubFunction sbf in subFunctions)
        {
            if (!string.IsNullOrEmpty(sbf.SymbolPosition))
            {
                Symbol symbol = symbols.FirstOrDefault(x => x.Id == sbf.SymbolId);

                if (symbol != null)
                {
                    AKZ = "";
                    OKZ = "";
                    ProjectSymbols model = new ProjectSymbols
                    {
                        SVG = symbol.SvgString,
                        ProjectId = projectId,
                        SubFunctionId = sbf.Id,
                        Symbol = sbf.Symbol,
                        SymbolId = sbf.SymbolId,
                        SymbolPosition = sbf.SymbolPosition,
                        SymbolTypeId = sbf.SymbolTypeId,
                        SymbolVariant = sbf.SymbolVariant
                    };

                    if (string.IsNullOrEmpty(sbf.Function.FunctionSign))
                    { AKZ = sbf.Function.FunctionGroup.FunctionSign; }
                    else { AKZ = sbf.Function.FunctionSign; }

                    if (string.IsNullOrEmpty(sbf.Function.LocationSign))
                    { OKZ = sbf.Function.FunctionGroup.LocationSign; }
                    else { OKZ = sbf.Function.LocationSign; }

                    model.BMK = "=" + AKZ + "+" + OKZ;

                    projectSymbols.Add(model);
                }
            }
        }

        return projectSymbols;
    }

Actually the wrong data is loaded here:

AKZ = sbf.Function.FunctionGroup.FunctionSign;

As I could see in debug mode, if I load this FunctionSign, its value is different from the one in the table.

Thanks

Upvotes: 0

Views: 626

Answers (1)

rjs123431
rjs123431

Reputation: 688

Your function GetProjectSymbols is a static so I assumed your db DbContext is also static. If the DbContext variable you use to save the data is not the same variable you use to load the data, then you need to reinitialize it before querying so it will get updated data from the database.

Put this line

db = new YourDbContext();

before

IEnumerable<Symbol> symbols = db.Symbols.ToList();

Upvotes: 1

Related Questions