3

目前在 .net 4.5 上使用 codefirst v5

我有 1 个“主”上下文,其中包括一个名为“Geonames”的位置表(下面的片段)

public class CoreContext : DbContext
            : base("Conn")
{
    public DbSet<GeoName> GeoNames { get; set; }
}

这里的参考只是为了能够管理表上的迁移。

站点将能够“插入”不同的位置提供程序,并且 geonames 表可以提供本地提供程序实现。提供者在网站的配置中设置,只需设置实际实现的类名和命名空间。它使用这样的接口(简化)

public interface ILocationProvider
{
    IEnumerable<Location> LocationsByName(String Name);
}    

具体实现如下(简化)

    public static ILocationProvider GetLocationProvider()
    {

        ILocationProvider Provider = null;

        Provider = Activator.CreateInstance(Type.GetType("{selected namespace.classname}")) as ILocationProvider;
        }
        catch (Exception ex)
        {
            throw ex;
        }

        if (Provider == null)
        {
            throw new NullReferenceException("Could not create an instance of the location Provider");
        }

        return Provider;
    }

具体实现如下(简化)

public class LocalLocationProvider : ILocationProvider
{
    public IEnumerable<Location> LocationsByName(string Name)
    {
        List<Location> locations = new List<Location>();

        using (var db = new LocalLocationContext())
        {   

            foreach (var item in db.GeoNames.Where(X => X.Name.Contains(Name)))
            {   
                locations.Add(
                    new Location { Id = item.Id, Name = item.Name, GeoLocation = item.GeoLocation }
                );
            }
        }

        return locations;
    }

    private class LocalLocationContext : DbContext
    {
        public LocalLocationContext()
            : base("Conn")
        {
            Database.SetInitializer<LocalLocationContext>(null);
        }

        public DbSet<GeoName> GeoNames { get; set; }
    }
}

具体的实现实例化得很好,我将方法称为“LocationsByName”..但问题就在这里。当下面的代码行执行时

foreach (var item in db.GeoNames.Where(X => X.Name.Contains(Name)))
{
  ..
}

我收到以下错误。

Exception Details: System.ComponentModel.Win32Exception: The wait operation timed out

[Win32Exception (0x80004005): The wait operation timed out]

    [SqlException (0x80131904): Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +1753986
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5296058
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +558
   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1682
   System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +59
   System.Data.SqlClient.SqlDataReader.get_MetaData() +90
   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +365
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite) +1379
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +175
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +53
   System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +134
   System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +41
   System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) +10
   System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) +437

我试图在不使用 'Activator.CreateInstance()' 方法的情况下实例化 'LocalLocationContext' 我可以很好地查询数据。

对我来说,很明显这与我如何获得位置提供程序的实际实现有关,只是无法发现错误或问题是什么。

提前致谢。

4

0 回答 0