";当';名称';指定了关键字";未定义名称

本文关键字:quot 未定义 关键字 名称 | 更新日期: 2023-09-27 17:57:48

我一直在写一个小应用程序中遇到这个异常。我真的找不到原因,但我认为我做错了什么。

测试代码如下所示:

public string GenerateConnectionString()
    {
        var sqlBuilder = new SqlConnectionStringBuilder
        {
            DataSource = Properties.Resources.SERVER_NAME,
            InitialCatalog = Properties.Resources.DB_NAME,
            UserID = Properties.Resources.DB_USER,
            Password = Properties.Resources.DB_PW,
            MultipleActiveResultSets = true,
            ApplicationName = "EntityFramework",
            IntegratedSecurity = false
        };
        string providerString = sqlBuilder.ToString();
        var entityBuilder = new EntityConnectionStringBuilder
        {
            Provider = "System.Data.SqlClient",
            ProviderConnectionString = providerString,
            Metadata = @"res://*/PcsDb.csdl|res://*/PcsDb.ssdl|res://*/PcsDb.msl"
        };
        testconnection(entityBuilder.ToString());
        return entityBuilder.ToString();
    }
private void testconnection(string connectionString)
    {
        var con = new EntityConnection(connectionString);
        con.Open();
        con.Close();
        tabMitarbeiter mitarbeirter;
        using (var context = new PCSEntities(connectionString))
        {
            mitarbeirter = (from mitarbeiter in context.tabMitarbeiter select mitarbeiter).FirstOrDefault(
                        elt => elt.UserID == "santos");
        }
    }

在使用(mitarbiter=(from[…])时,我总是得到相同的异常:

Other keywords are not allowed when the 'Name' keyword is specified.

位于System.Data.EntityClient.EntityConnection.ChangeConnectionString(字符串newConnectionString)位于System.Data.EntityClient.EntityConnection.ctor(字符串连接字符串)位于System.Data.Entity.Interal.LazyInternalConnection.Initialize()位于System.Data.Entity.Interal.LazyInternalConnection.CreateObjectContextFromConnectionModel()位于System.Data.Entity.Interal.LazyInternalContext.InitialContext()位于System.Data.Entity.InteralContext.GetEntitySetAndBaseTypeForType(类型entityType)位于System.Data.Entity.Internal.Linq.InternalSet 1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet 1.get_InternalContext()位于System.Data.Entity.Infrastructure.DbQuery 1.System.Linq.IQueryable.get_Provider() at System.Linq.Queryable.Select[TSource,TResult](IQueryable 1源,表达式`1选择器)位于d:''Projects''GlauxSoft.PSS.WebService''PSDB''PCSDB.cs:line 134中的PCSDB.PCSDB.testconnection(字符串连接字符串)位于d:''Projects''GlauxSoft.PSS.WebService''PCSDB''PCSDB.cs:line 118中的PCSDB.PCSDB.GenerateConnectionString()位于d:''Projects''GlauxSoft.PCS.WebService''PSDB''PCSDB.cs:line 15中的PCSDB.PCSDB.ctor()在d:''Projects''GlauxSoft.PCS.WebService''Jira-Sync''JiraSync.cs:line 14中的JiraSync.JiraSync.ctor(字符串名称,日期时间日期)在D:''Projects''GlauxSoft.PCS.WebService''PcsService.JiraSync(字符串用户名,日期时间数据)中在SyncInvokeJiraSync(Object,Object[],Object[])位于System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(对象实例、对象[]输入、对象[]和输出)在System.ServiceModel.Dispatcher.DispatchOperationRuntime.IInvokeBegin(MessageRpc&rpc)

我确实在上下文类中创建了一个新的构造函数,这样我就可以传递连接字符串。EntityConnection可以打开和关闭,但一旦我尝试获取一些数据,就会出现异常。

正如我一开始所说,我很确定这是我的错误,因为这是我第一次在EntityFramework中使用动态连接。

";当';名称';指定了关键字";未定义名称

尝试简化连接Metadata:

Metadata = @"res://*/"

此外,请确保您的资源与Model具有相同的名称。