挂在System.Data.Entity.DbSet.Add()中 [英] Hang in System.Data.Entity.DbSet.Add()

查看:77
本文介绍了挂在System.Data.Entity.DbSet.Add()中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注使用ASP.NET Web API 2.2 示例创建OData v4端点, Post()方法实现看起来像

I am following the Create an OData v4 Endpoint Using ASP.NET Web API 2.2 sample, and the Post() method implementation looks like this:

public async Task<IHttpActionResult> Post(Product product)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }
    db.Products.Add(product);
    await db.SaveChangesAsync();
    return Created(product);
}

但是,当收到发布请求时,产品.Add()挂起,永不返回。

However, when a Post request is received, Products.Add() hangs and never returns. No exception is thrown.

在WinDBG下,我可以看到以下调用堆栈:

Under WinDBG I can see the following callstack:

System.Threading.Thread.Sleep
System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover
System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist
System.Data.SqlClient.SqlInternalConnectionTds..ctor
System.Data.SqlClient.SqlConnectionFactory.CreateConnection
System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection
System.Data.ProviderBase.DbConnectionPool.CreateObject
System.Data.ProviderBase.DbConnectionPool.UserCreateRequest
System.Data.ProviderBase.DbConnectionPool.TryGetConnection
System.Data.ProviderBase.DbConnectionPool.TryGetConnection
System.Data.ProviderBase.DbConnectionFactory.TryGetConnection
System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal
System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection
System.Data.SqlClient.SqlConnection.TryOpenInner
System.Runtime.InteropServices.SafeHandle.Dispose
System.Data.SqlClient.SqlConnection.TryOpen
System.Data.SqlClient.SqlConnection.Open
System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute
System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection
System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection
System.Data.Entity.SqlServer.SqlProviderServices.GetDbProviderManifestToken
System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken
System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked
System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.ResolveManifestToken
System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo
System.Data.Entity.DbModelBuilder.Build
System.Data.Entity.Internal.LazyInternalContext.CreateModel
System.Data.Entity.Internal.LazyInternalContext.InitializeContext
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType
System.Data.Entity.DbSet.Add
ProductService.Controllers.ProductsController.Post

我正在使用 EntityFramework 6.1.3

如果 Add()方法是一种与数据库建立连接的方法, * Async()方法?

If the Add() method is the one creating the connection with the database, should it be an *Async() method?

更新:

将代码从ASP.NET移植到控制台应用程序后,我捕获了以下异常:

After porting the code from ASP.NET to Console Application, I caught the following exception:


EntityFramework.dll中发生类型为'System.Data.SqlClient.SqlException'的未处理异常

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll

附加信息:建立连接时发生与网络相关或特定于实例的错误到SQL Server。服务器未找到或无法访问。验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (提供者:SQL网络接口,错误:50-发生本地数据库运行时错误。无法创建自动实例。有关错误详细信息,请参见Windows应用程序事件日志。)

Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.)


推荐答案

问题是教程连接字符串对Visual Studio 2015不再有效; Data Source =(localdb)\v11.0 需要替换为 Data Source =(localdb)\mssqllocaldb

The problem is that the tutorial connection string is not valid anymore for Visual Studio 2015; Data Source=(localdb)\v11.0 needs to be replaced with Data Source=(localdb)\mssqllocaldb.

即:

<add
  name="ProductsContext"
  connectionString="Data Source=(localdb)\mssqllocaldb; 
    Initial Catalog=ProductsContext; Integrated Security=True;
    MultipleActiveResultSets=True; 
    AttachDbFilename=|DataDirectory|ProductsContext.mdf"
  providerName="System.Data.SqlClient" />

这篇关于挂在System.Data.Entity.DbSet.Add()中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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