【问题标题】:Web Service querying database with LINQ fails on one computer, not the other使用 LINQ 查询数据库的 Web 服务在一台计算机上失败,而不是在另一台计算机上
【发布时间】:2012-11-09 10:31:44
【问题描述】:

我在我的计算机上本地开发了一个小型 Web 窗体应用程序,它运行良好,但是当我将它移动到另一台计算机时,我使用 AJAX 查询的 Web 服务失败了。

以下是相关代码:

/*Search for Event.country by name*/
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string[] GetVenues(string searchterm)
    {
        var _db = new MyApp.Models.EventContext();

        var myResult = from e in _db.Events
                       where e.venue.Contains(searchterm)
                       select e.venue;

        return myResult.Distinct().ToArray();
    }

我怀疑我的问题出在 EventContext 类中的 OnModelCreating 方法上:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Event>().ToTable("events");
    }

我从本地计算机备份数据库并在另一台计算机上恢复它,所以我怀疑我在使用 OnModelCreating 时做错了,因为表已经存在。我的假设是否正确?

这是我收到的错误消息,作为 Firebug 查看的 Web 服务的响应:

{"Message":"An exception occurred while initializing the database. See the InnerException for details.","StackTrace":" 
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) 
 at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
 at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase> b__4(InternalContext c)
 at System.Data.Entity.Internal.RetryAction'1.PerformAction(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action'1 action)
 at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
 at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
 at System.Data.Entity.Internal.Linq.InternalSet'1.Initialize()
 at System.Data.Entity.Internal.Linq.InternalSet'1.get_InternalContext()
at System.Data.Entity.Infrastructure.DbQuery'1.System.Linq.IQueryable.get_Provider()
 at System.Linq.Queryable.Where[TSource](IQueryable'1 source, Expression'1 predicate)
at MyApp.WebServices.AutoComplete.GetVenues(String searchterm)","ExceptionType":"System.Data.DataException"}

【问题讨论】:

  • 消息说您应该检查内部异常以了解更多详细信息。这样做怎么样?
  • 我以为我在我的问题中写了它,但是当我重组它时我一定丢失了它:代码在我朋友的计算机上运行,​​我无权在那里调试它。
  • 添加记录器并从您的朋友那里获取日志文件也会有所帮助。

标签: c# linq webforms


【解决方案1】:

很难从所提供的数据中明确问题的根源。

您的 cmets 声明您无权在您朋友的计算机上调试应用程序。您是否在 Visual Studio 中尝试过 Remote Debugging

您可能还想检查您的数据库中是否定义了可为空的类型,或者可能与数据库默认值不同的列值。这表明数据存在问题,以及DataException

的原因

【讨论】:

    猜你喜欢
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多