在LINQ to SQL datacontext中插入项目-在应用程序重新启动之前不会出现 [英] Inserting item in LINQ to SQL datacontext - doesn't appear until application restart

查看:48
本文介绍了在LINQ to SQL datacontext中插入项目-在应用程序重新启动之前不会出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET MVC(使用候选版本)应用程序,该应用程序与类库一起使用,该类库除其他外还使用LINQ to SQL进行数据持久化.

app/db维护的一件事是文件夹"的概念-类似于磁盘文件夹,但具有仅存在于数据库中的层次结构.从用户的角度来看,其他数据库对象也位于这些文件夹中.

类库中的每个类都有自己的对DataContext对象的静态引用.此外,MVC控制器每个都有其自己的DataContext对象.

我有两个返回JSON数据的动作.一种是"GetFoldersJSON",它以适合选择(下拉)列表的格式返回文件夹结构.另一个是"AddFolderJSON",它接受一些表单数据,将新的Folder插入数据库,然后返回GetFoldersJSON()以将新的文件夹列表发送到客户端.一旦创建了新文件夹,客户端将通过这些动作使用AJAX刷新下拉列表.

此操作的流程如下:

  1. AddFolderJSON()操作-使用新文件夹名称,父文件夹等获取表单信息.

  2. 创建新的Folder对象.执行一个dataContext.Folders.InsertOnSubmit(newFolder);执行一个dataContext.SubmitChanges();执行一个dataContext.Refresh(OverwriteThingy,dataContext.Folders);GetFoldersJSON() 返回结果

  3. GetFoldersJSON()使用驻留在Folder定义中的函数-扩展OR映射的文件夹对象的局部类.此功能使用如下代码将文件夹和子文件夹递归添加到平面列表中:

      var rootFolders =来自db中的f其中f.ParentFolder == null订单名称选择f; 

db当然是本地的静态DataContext引用.

然后是一个简单的循环:

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

然后,AddFolderContents()函数将当前文件夹添加到列表中,并继续为当前文件夹的每个子文件夹调用自身,从而创建层次结构.

这对于数据库中已有的数据可以正常工作,但是当我们在创建新文件夹后运行此过程时,不会显示新文件夹.如果我们刷新页面,它仍然不会显示.在重新启动应用程序之前,它实际上不会显示.

我试图在几乎所有地方进行Refresh()调用,在这种情况下似乎没有任何作用.

是否有一种方法可以将LINQ告知SQL:嘿,我知道您在想什么,但是请全部删除,然后立即从数据库中获取所有数据!"

我以前曾听说过这种感觉,但找不到此处描述的东西.

解决方案

是否有一种方法可以将LINQ告知SQL:嘿,我知道您在想什么,但是请全部删除,然后立即从数据库中获取所有数据!"

db =新的customDataContext();

db当然是本地的静态DataContext引用

是的,当心.ASP.NET是多线程的,您不想共享不安全的DataContext实例.


似乎您正在通过其父母访问子文件夹.DataContext缓存已读取的对象的状态,并在进一步查询时将这些相同的实例返回给您.需要通知父实例它有一个新的子实例.签出生成的ParentFolder属性,看看它是否通知父文件夹.如果是这样,则说明您在做生意:

  child.ParentFolder =父级; 

否则,您需要这样做:

  parent.Children.Add(child); 

请勿通过id进行操作,在这种情况下,自动生成的代码不会通知父对象.

  child.ParentFolderId = parent.Id;//坏,坏了,不要做 

在您的SubmitChanges调用之前执行此操作.

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

One of the thing the app/db maintains, is the concept of a "Folder" - like a disk folder, but with a hierarchy that only exists in the database. Other database objects live in these folders, seen from the perspective of the user.

Each class in the class library has its own static reference to my DataContext object. In addition, the MVC controller each has it's own DataContext object.

I have two Actions returning JSON data. One is a "GetFoldersJSON" which returns the folder structure in a format suitable for a select (dropdown) list. The other one is "AddFolderJSON" which accepts some form data, inserts a new Folder into the database, and then returns GetFoldersJSON() for sending the new folder list to the client. The client uses AJAX with these actions to refresh the dropdownlists once a new folder is created.

The flow of this operation is as such:

  1. AddFolderJSON() action - Get the form information with new folder name, parent folder etc.

  2. Create new Folder object. Perform a dataContext.Folders.InsertOnSubmit(newFolder); Perform a dataContext.SubmitChanges(); Perform a dataContext.Refresh(OverwriteThingy, dataContext.Folders); return result of GetFoldersJSON()

  3. GetFoldersJSON() uses a function which resides in the Folder definition - a partial class extending the OR-mapped folder object. This function recursively adds folders and subfolders to a flat list using code like this:

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

db is of course the local, static DataContext reference.

Then a simple loop:

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

The AddFolderContents() function then adds the current folder to the list, and proceeds to call itself for each subfolder of the current folder, thus creating a hierarchy.

This works fine for data already in the database, but when we run this procedure after creating a new Folder, the new folder does not show up. If we refresh the page, it still doesn't show up. It doesn't actually show up until the application is restarted.

I have tried to do Refresh() calls just about everywhere, and it seems to have no effect in this case.

Is there a way to tell LINQ to SQL "Hey, I know what you're thinking, but DROP IT ALL, and just get ALL the data from the database, NOW!"?

I have a nagging feeling I've heard about this before, but couldn't find it described here.

解决方案

Is there a way to tell LINQ to SQL "Hey, I know what you're thinking, but DROP IT ALL, and just get ALL the data from the database, NOW!"?

db = new customDataContext();

db is of course the local, static DataContext reference

Yeah, watch out there. ASP.NET is multithreaded and you don't want to share an unsafe-for-threading DataContext instance.


It seems as though you are accessing children folders through their parents. DataContext caches the state of objects it has already read and returns those same instances to you on further querying. The parent instance needs to be notified that it has a new child instance. Check out the generated ParentFolder property and see if it notifies the parent folder. If so, you're in business:

child.ParentFolder = parent;

If not, you need to do it this way:

parent.Children.Add(child);

Do not do it by id, the autogenerated code does not notify the parent object in this case.

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

Do this before your SubmitChanges call.

这篇关于在LINQ to SQL datacontext中插入项目-在应用程序重新启动之前不会出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆