"当'姓名'时,不允许使用其他关键字。已指定关键字。"没有名字定义

时间:2014-04-11 08:07:38

标签: c# web-services entity-framework

我一直在我写的小应用程序中得到这个异常。我无法找到原因,但认为我做错了。

测试代码如下:

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");
        }
    }

在使用中(mitarbeiter =(来自[...])我总是得到同样的例外:

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

at System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)      在System.Data.EntityClient.EntityConnection..ctor(String connectionString)      在System.Data.Entity.Internal.LazyInternalConnection.Initialize()      在System.Data.Entity.Internal.LazyInternalConnection.CreateObjectContextFromConnectionModel()      在System.Data.Entity.Internal.LazyInternalContext.InitializeContext()      在System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type 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.PCS.WebService \ PCSDB \ PcsDb.cs中的PCSDB.PcsDb.testconnection(String connectionString):第134行      位于d:\ Projects \ GlauxSoft.PCS.WebService \ PCSDB \ PcsDb.cs中的PCSDB.PcsDb.GenerateConnectionString():第118行      位于d:\ Projects \ GlauxSoft.PCS.WebService \ PCSDB \ PcsDb.cs中的PCSDB.PcsDb..ctor():第15行      在J:\ Projects \ GlauxSoft.PCS.WebService \ Jira Sync \ JiraSync.cs中的JiraSync.JiraSync..ctor(字符串名称,日期时间日期):第14行      位于D:\ Projects \ GlauxSoft.PCS.WebService \ GlauxSoft.PCS.WebService \ PcsService.svc.vb中的GlauxSoft.PCS.WebService.PcsService.JiraSync(String userName,DateTime datum):第799行      在SyncInvokeJiraSync(Object,Object [],Object [])      在System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(对象实例,对象[]输入,对象[]和输出)      在System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)

我确实在上下文类中创建了一个新的构造函数,以便我可以传递connectionstring。并且可以打开和关闭EntityConnection,但是一旦我尝试获取某些数据,我就会得到异常。

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

1 个答案:

答案 0 :(得分:1)

尝试简化连接Metadata

Metadata = @"res://*/"

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