相关疑难解决方法(0)

已经有一个与此命令关联的打开DataReader,必须先关闭它

我有这个查询,我在这个函数中得到错误:

var accounts = from account in context.Accounts
               from guranteer in account.Gurantors
               select new AccountsReport
               {
                   CreditRegistryId = account.CreditRegistryId,
                   AccountNumber = account.AccountNo,
                   DateOpened = account.DateOpened,
               };

 return accounts.AsEnumerable()
                .Select((account, index) => new AccountsReport()
                    {
                        RecordNumber = FormattedRowNumber(account, index + 1),
                        CreditRegistryId = account.CreditRegistryId,
                        DateLastUpdated = DateLastUpdated(account.CreditRegistryId, account.AccountNumber),
                        AccountNumber = FormattedAccountNumber(account.AccountType, account.AccountNumber)
                    })
                .OrderBy(c=>c.FormattedRecordNumber)
                .ThenByDescending(c => c.StateChangeDate);


public DateTime DateLastUpdated(long creditorRegistryId, string accountNo)
{
    return (from h in context.AccountHistory
            where h.CreditorRegistryId == creditorRegistryId && h.AccountNo == accountNo
            select h.LastUpdated).Max();
}
Run Code Online (Sandbox Code Playgroud)

错误是: …

c# entity-framework entity-framework-4

592
推荐指数
7
解决办法
41万
查看次数

实体框架:已经有一个与此命令关联的开放DataReader

我正在使用实体框架,偶尔我会得到这个错误.

EntityCommandExecutionException
{"There is already an open DataReader associated with this Command which must be closed first."}
   at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands...
Run Code Online (Sandbox Code Playgroud)

即使我没有做任何手动连接管理.

这个错误间歇性地发生.

触发错误的代码(为了便于阅读而缩短):

        if (critera.FromDate > x) {
            t= _tEntitites.T.Where(predicate).ToList();
        }
        else {
            t= new List<T>(_tEntitites.TA.Where(historicPredicate).ToList());
        }
Run Code Online (Sandbox Code Playgroud)

使用Dispose模式以便每次都打开新连接.

using (_tEntitites = new TEntities(GetEntityConnection())) {

    if (critera.FromDate > x) {
        t= _tEntitites.T.Where(predicate).ToList();
    }
    else {
        t= new List<T>(_tEntitites.TA.Where(historicPredicate).ToList());
    }

}
Run Code Online (Sandbox Code Playgroud)

仍有问题

如果连接已经打开,EF为什么不重用连接?

linq entity-framework sql-server-2008

279
推荐指数
6
解决办法
15万
查看次数

不允许新事务,因为在会话LINQ To Entity中运行其他线程

关于为什么这可能会破裂的任何想法?

foreach (var p in pp)
{
    ProjectFiles projectFile = (ProjectFiles)p;
    projectFile.Status = Constants.ProjectFiles_ERROR;
    projectFile.DateLastUpdated = DateTime.Now;
    context.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)

我读到问题的解决方法是在foreach循环之前一次性检索结果.

但我不这样做吗?"pp"是我案例中的结果集合

c# linq linq-to-entities

140
推荐指数
2
解决办法
8万
查看次数

实体框架大数据集,内存不足异常

我正在处理一个非常大的数据集,大约有200万条记录.我有下面的代码,但是在它处理了三个批次,大约600,000条记录后得到了一个内存不足的例外.我理解,因为它循环遍历每个批处理实体框架的延迟加载,然后尝试将完整的200万条记录构建到内存中.有没有办法卸载我处理过的批次?

ModelContext dbContext = new ModelContext();
IEnumerable<IEnumerable<Town>> towns = dbContext.Towns.OrderBy(t => t.TownID).Batch(200000);
foreach (var batch in towns)
{
    SearchClient.Instance.IndexMany(batch, SearchClient.Instance.Settings.DefaultIndex, "Town", new SimpleBulkParameters() { Refresh = false });
}
Run Code Online (Sandbox Code Playgroud)

注意:批处理方法来自此项目:https://code.google.com/p/morelinq/

搜索客户端是这样的:https://github.com/Mpdreamz/NEST

c# entity-framework

27
推荐指数
1
解决办法
3万
查看次数

不允许新事务,因为会话中正在运行其他线程

获取"不允许新事务,因为会话中正在运行其他线程".

它与foreach循环或任何人通常与此消息一起出现问题无关.

我在整个请求中使用带有repositoy模式和公共上下文的EF4.有些事情发生了,无法准确确定是什么,只要我尝试使用上下文保存变更,跨请求,我就会收到此消息,并且只有在我回收应用程序池后才会消失.

我关闭连接了吗?我该怎么说?我是否为每个请求使用了新的上下文?是.

这是怎么回事?有解决方法吗?

编辑:(上下文工厂)

    private static Dictionary<string, CoinEntities> _instances;

    public static CoinEntities DefaultInstance
    {
        get
        {
            if (HttpContext.Current == null)
            { //todo: mock instead. testing.
                if (!Instances.ContainsKey("_TEST"))
                    Instances["_TEST"] = new CoinEntities();
                return Instances["_TEST"];
            }

            if (!Instances.ContainsKey("_DEFAULT"))
                Instances["_DEFAULT"] = new CoinEntities();

            return Instances["_DEFAULT"];
        }
    }
Run Code Online (Sandbox Code Playgroud)

ado.net entity-framework-4

8
推荐指数
1
解决办法
3万
查看次数

经常保存实体框架

使用实体框架4.3.1数据库首先,经常提交/保存对象更改到数据库的好方法是什么?在下面,我想在quickbooks电话之后立即保存发票,而不是等待发布所有发票的风险.但是,我不能每次在循环中调用SaveChanges,它会抛出异常.

在每个对象上使用.Save()方法会很方便,也许有一个很好的方法可以做到这一点?

var unpostedInvoices = entities.GetUnpostedInvoices();
foreach (Invoice invoice in unpostedInvoices)
{
    // this takes a long time
    var invoiceDto = quickbooks.PostInvoice(invoice);

    invoice.Posted = true;
    invoice.TransactionId = invoiceDto.TransactionId;

    // I'd like to save here rather than after the foreach loop, but this will fail
    //entities.SaveChanges();
}

// this works, but I don't want to risk waiting this long to save
entities.SaveChanges();
Run Code Online (Sandbox Code Playgroud)

这是在循环中调用SaveChanges()时抛出的异常.

New transaction is not allowed because there are other threads running in the session.

at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean …
Run Code Online (Sandbox Code Playgroud)

.net entity-framework

6
推荐指数
1
解决办法
3024
查看次数