鉴于下面的类,在 Visual Studio 中启动网站在调用时会出现以下错误home/index
(这实际上是内部异常)。
A transport-level error has occurred when receiving results from the server. (provider: Shared Memory Provider, error: 0 - The handle is invalid.)
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParserStateObject.ReadSniError(TdsParserStateObject stateObj, UInt32 error)
at System.Data.SqlClient.TdsParserStateObject.ReadSni(DbAsyncResult asyncResult, TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParserStateObject.ReadNetworkPacket()
at System.Data.SqlClient.TdsParserStateObject.ReadBuffer()
at System.Data.SqlClient.TdsParserStateObject.ReadByte()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
但是,如果我在本地发布完全相同的代码并使用 WebMatrix 访问该站点,它就可以正常工作。
如果有区别的话,我也在使用 SQL Server 2008 Express。请告知是否需要更多信息。
控制器
public class HomeController : BaseController
{
public ActionResult Index()
{
var candidates = BusinessObj.AllOf<Candidate>().ToList();
return View(candidates);
}
}
基本控制器
public class BaseController : Controller
{
protected ITestBusinessObject BusinessObj { get; set; }
public BaseController()
{
BusinessObj = new Business.TestBusinessObj();
}
}
TestBusinessObj(实体是一个 EF edmx 类)
public class TestBusiness : ITestBusinessObject
{
private Entities DB = new Entities();
private string ContainerName = "Entities";
public IEnumerable<T> AllOf<T>() where T : class, new()
{
return DB.CreateObjectSet<T>(ContainerName.GetEntitySetName<T>());
}
private static string GetEntitySetName<T>(this string containerName) where T : new()
{
var name = new T().GetType().Name;
if (name.EndsWith("y"))
{
name = name.Substring(0, name.Length - 1) + "ies";
}
else
{
name = name + "s";
}
return string.Format("{0}.{1}", containerName, name);
}
}