Insert item in LINQ to SQL datacontext - not displayed until application restarts

I have an ASP.NET MVC application (using a release candidate) that works with a class library that, among other things, uses LINQ to SQL to save data.

One of the things supported by the / db application is the concept of "Folder" - like a disk folder, but with a hierarchy that exists only in the database. Other database objects live in these folders, visible from the user's point of view.

Each class library class has its own static link to my DataContext object. In addition, each MVC has its own DataContext.

I have two actions returning JSON data. One of them is "GetFoldersJSON", which returns the folder structure in a format suitable for the select list (drop-down list). The other is "AddFolderJSON", which takes some form data, inserts a new new folder into the database and returns GetFoldersJSON () to send a new folder list to the client. The client uses AJAX with these actions to update dropdownlists after creating a new folder.

The flow of this operation is as follows:

  • Action AddFolderJSON (). Get form information with a new folder name, parent folder, etc.

  • Create a new Folder object. Run dataContext.Folders.InsertOnSubmit (newFolder); Run dataContext.SubmitChanges (); Execute dataContext.Refresh (OverwriteThingy, dataContext.Folders); return result from GetFoldersJSON ()

  • GetFoldersJSON() , - , OR. , :

    var rootFolders = from f in db.Folders
                      where f.ParentFolder == null
                      orderby f.name
                      select f;
    

db - , , DataContext.

:

    foreach (var fldr in rootFolders)
    {
        AddFolderContents(list, fldr);
    }

AddFolderContents() , .

, , , . , . , .

Refresh() , .

LINQ to SQL ", , , DROP IT ALL, , !"?

, , .

+1
3

LINQ to SQL ", , , DROP IT ALL, , !"?

db = customDataContext();

db - , , DataContext

, . ASP.NET , DataContext, .


, . DataContext , , . , . ParentFolder , . , :

child.ParentFolder = parent;

, :

parent.Children.Add(child);

id, .

child.ParentFolderId = parent.Id; //bad, broken, do not do

SubmitChanges.

+2

, DataContext ( ) , .

DataContext " "... , , . ? , , .

MSDN, Microsoft :

DataContext , . , " ", , .

DataContext " ", . DataContext - . LINQ to SQL DataContext , .

, 10 LINQ Myths ( # ) datacontext:

, , DataContext, .

DataContext , DataContext .

, DataContext , DataContext .

+4

: -

, DataContect, .

db.Refresh(System.Data.Linq.RefreshMode.KeepChanges, myObjectsCollection)

myObjectionCollection - Linq, datatase. (.. ).

! -, EF - . .

, . Linq ( ), .

....

+1
source

Source: https://habr.com/ru/post/1703241/


All Articles