1

I am cloning user's information, security, departments and several other tables of information to create a new user with same setup. This information is being displayed through several DGV's. When I use a normal linq to context:

var x = (from ctx in context.UserInfo
         where ctx.UserID == userID
         select ctx);

The information display almost instantaneously, but does not display the added user, unless I call SaveChanges() which I'm trying not to do.

Then I switched to using local linq:

context.UserInfo.Load();
var x = (from ctx in context.UserInfo.Local
         where ctx.UserID == userID
         select ctx);

However, several tables I am querying have over 30k entries and the .Local call significantly slows down the process of displaying the information. From what I have researched this happens due to validation tracking.

Then I tried using the change tracker:

context.UserInfo.Load();
var x = (from ctx in context.ChangeTracker.Entries<UserInfo>()
         where ctx.Entity.UserID == userID && ctx.State == EntityState.Unchanged ||
            ctx.State == Entity.State.Added || ctx.State == EntityState.Modified
         select ctx.Entity)

This gave the same poor performance as the Local call.

Is there anyway to quickly display added objects and not display deleted objects without having to call Local or is there a way to speed up Local?

I have done a fair amount of research and worked on this problems for a few days, trying to figure it out on my own, but I'm going around in circles now.

I have thought about creating a copy of the context, before changes are made. Saving the changes as they happen, and if the user decides to cancel out the creation of the cloned user, calling up the original context and saving it, to restore the original context, but I would consider that a last resort.

The project is being done in C# .net 4.5 and Entity Framework 5.0 on a Windows Form

EDIT:

To give more code on how query's are implemented:

I have a loadData() method that initializes the data I will be using.

public void loadData()
{
    var x = (from ctx in context.UserInfo
             select ctx).ToList();
}

I believe this would do the same thing as the above code

public void loadData()
{
    context.UserInfo.load();
}

Then the query is called: This is fast query, but does not show context changes

public List<UserInfo> getUserInfo()
{
    var user = (from ctx in context.UserInfo
                where ctx.UserID == userID
                select ctx).ToList();

    return user;
}

This query is slow, but shows changes

public List<UserInfo> getUserInfo()
{
    var user = (from ctx in context.UserInfo.Local
                where ctx.UserID == userID
                select ctx).ToList();

    return user;
}
4

2 回答 2

1

我打算在这里进行远景 - 我认为延迟低于 EF 在初始化模型缓存时的标准延迟。有关更多详细信息,请参阅我对这个问题的回答。您需要修改代码以确定...

于 2013-06-18T17:30:48.437 回答
0

最初,我不知道 ToList() 的行为方式与 Load() 相同。有了这个,我改变了我的代码,不再使用我的 loadData() 方法,而是只初始化我需要的部分信息。

public List<UserInfo>(int userID 1)
{
    var data = (from ctx in context.UserInfo
                where ctx.UserID == userID
                select ctx).ToList();

    List<UserInfo> user = (from ctx in context.UserInfo.Local
                           where ctx.UserID == userID
                           select ctx).ToList();

    return user;
}

此更改使我的查询几乎可以立即运行,并且仍然跟踪上下文中的添加/删除/修改。我在此方法中看到的唯一缺点是,如果将大量对象加载到 UserInfo.Local 中,它将开始变慢,尽管在我的情况下这很可能不会发生。

于 2013-06-18T21:27:38.557 回答