'附近的语法不正确('。与实体框架的数据库连接

时间:2014-08-18 09:04:10

标签: c# asp.net-mvc entity-framework

我有 ASP.NET MVC 应用程序,它在我的本地服务器上运行良好。当我将其部署到IIS(v7.0)中的 Windows Server 2008 Standard 时,应用程序正在运行属性(我可以看到要登录的面板),但是当我想要登录时,我有这样的错误。我还有页面联系人,当我想运行它时,我也有同样的错误 我使用 ADO.NET实体数据模型添加Model.edmx。

代码很简单:

在控制器中:

private ImportEntities Entity = new ImportEntities();
public ActionResult Contact()
    {
        var dane = Entity.Contact.FirstOrDefault();
        return View("Contact",dane);
    }

Web.config中的连接字符串:

<add name="ImportEntities" connectionString="metadata=res://*/Models.Model.csdl|res://*/Models.Model.ssdl|res://*/Models.Model.msl;
 provider=System.Data.SqlClient;
 provider connection string=&quot;data source=AAA\aaa;
      initial catalog=Database_name;
      integrated security=True;
      MultipleActiveResultSets=True;
      App=EntityFramework&quot;"
 providerName="System.Data.EntityClient" />

错误:

Server Error in '/MyApp' Application.
Line 1: Incorrect syntax near '('. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Line 1: Incorrect syntax near '('.

Source Error: 
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 


Stack Trace: 
[SqlException (0x80131904): Line 1: Incorrect syntax near '('.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +1767866
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5352418
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +244
   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1691
   System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +61
   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, SqlDataReader ds) +1406
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +177
   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

[EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details.]
   System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) +507
   System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute(ObjectContext context, ObjectParameterCollection parameterValues) +730
   System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) +131
   System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +36
   System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 source) +152
   System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1(IEnumerable`1 sequence) +41
   System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle(IEnumerable`1 query, Expression queryRoot) +59
   System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute(Expression expression) +133
   System.Data.Entity.Internal.Linq.DbQueryProvider.Execute(Expression expression) +87
   System.Linq.Queryable.FirstOrDefault(IQueryable`1 source) +251
   MyApp.Controllers.HomeController.Contact() in c:\Users\mon\Documents\Visual Studio 2013\Projects\MyApp\MyApp\Controllers\HomeController.cs:39
   lambda_method(Closure , ControllerBase , Object[] ) +62
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +182
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
   System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +28
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +10
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +50
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
   System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +58
   System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +225
   System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +50
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
   System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +24
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +99
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +50
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +39
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +25
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +31
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9514928
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155



--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18446 

我不明白:当我的应用程序与'''使用某些字符串连接的时刻在哪里?我无法安装任何其他程序在服务器上调试它并检查错误。

你有什么想法可以出错吗?

======
在Model.edmx中 - &gt; Model.tt - &gt; Contact.cs

namespace MyApp.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Contact
    {
        public int id { get; set; }
        public string phone { get; set; }
        public string email { get; set; }
        public string address { get; set; }
        public string page { get; set; }
    }
}

======
我发现sql查询使用我的应用程序和@Paul_Zahra帮助。

SELECT TOP (1) 
[c].[id] AS [id], 
[c].[phone] AS [phone], 
[c].[mail] AS [mail], 
[c].[address] AS [address], 
[c].[page] AS [page]
FROM [dbo].[Contact] AS [c]

现在我可以看到第一行有问题。我记得我不应该(1),只需 1 。你知道如何改变吗?

2 个答案:

答案 0 :(得分:1)

这可能是由于您“共享”您的上下文...尝试此... ...(通常最好使用工作单元方法来创建和使用您的数据库上下文,除非您真的知道您在上下文中做了什么并且通常愿意微观管理它... ...

public ActionResult Contact()
{
    Contact dane = null;

    using (ImportEntities ctx = new ImportEntities())
    {
        ctx.Connection.Open();

        dane = ctx.Contact.FirstOrDefault()

        ctx.Connection.Close();
    }

    return View("Contact",dane);
}

修改

您的问题可能源于DbProviderInfo.ProviderManifestToken; “标识正在使用的数据库服务器版本的字符串。例如,SQL Server提供程序对SQL Server 2008使用字符串”2008“。这不能为空,但可能为空。”

您的本地(工作正常)和SQL Server的服务器版本是否相同?我猜不是。

令牌在SSDL中指定。 See this post for some interesting insights into what is a pretty big problem with EF for some

答案 1 :(得分:0)

connectionString="metadata=res:

你没有关闭那个&#34;