“(”附近的语法不正确.使用实体框架连接到数据库

本文关键字:实体 框架 连接 数据库 不正确 语法 | 更新日期: 2023-09-27 18:18:31

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

代码很简单:

在控制器中:

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 -> Model.tt -> 联系人中.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。你知道怎么改变它吗?

“(”附近的语法不正确.使用实体框架连接到数据库

这可能是由您"共享"上下文引起的...试试这个...(通常最好使用工作单元方法来创建和使用数据库上下文,除非您真的知道自己对上下文做了什么,并且通常愿意对其进行微观管理(...

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 中指定。请参阅这篇文章,了解一些有趣的见解,了解 EF 的一个相当大的问题

connectionString="metadata=res:

你没有关闭那个">