Web.config - 不支持关键字:'workstation id'

时间:2014-07-24 14:33:21

标签: c# entity-framework connection-string ef-database-first sqlclient

我在somee.com的ftp服务器上上传了一个用ASP.NET MVC编写的网站。

在web.config中,我遇到了这个部分的问题:

<connectionStrings>
  <add name="Entities" connectionString="workstation id=ICookDB.mssql.somee.com;packet size=4096;user id=***;pwd=***;data source=ICookDB.mssql.somee.com;persist security info=False;initial catalog=ICookDB" providerName="System.Data.EntityClient" />
</connectionStrings>

对于某些网页,我发现了这个错误:

[ArgumentException: Keyword not supported: 'workstation id'.]
System.Data.EntityClient.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Hashtable synonyms) +8420372
System.Data.EntityClient.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms) +82
System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString) +102
System.Data.EntityClient.EntityConnection..ctor(String connectionString) +43
System.Data.Entity.Internal.LazyInternalConnection.InitializeFromConnectionStringSetting(ConnectionStringSettings appConfigConnection) +78
System.Data.Entity.Internal.LazyInternalConnection.TryInitializeFromAppConfig(String name, AppConfig config) +32
System.Data.Entity.Internal.LazyInternalConnection.Initialize() +127
System.Data.Entity.Internal.LazyInternalConnection.get_ProviderName() +13
System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +346
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +18
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +52

2 个答案:

答案 0 :(得分:1)

由于EntityClient的使用,您似乎正在使用Entity Framework和.edmx模型

EntityClient使用它自己的格式嵌入实际的SqlClient连接字符串。

格式如下:

<connectionStrings>
    <add name="AdventureWorksEntities"
        connectionString="metadata=.\AdventureWorks.csdl|.\AdventureWorks.ssdl|.\AdventureWorks.msl;
                          provider=System.Data.SqlClient;
                          provider connection string='Data Source=localhost;Initial Catalog=AdventureWorks;Integrated Security=True;Connection Timeout=60;multipleactiveresultsets=true'"
        providerName="System.Data.EntityClient" />
</connectionStrings>

所以在你的情况下你可以尝试这样的事情

<connectionStrings>
    <add name="Entities"
        connectionString="metadata=<Path-to-your-CSDL>.csdl|<Path-to-your-SSDL>.ssdl|<Path-to-your-MSL>.msl;
                          provider=System.Data.SqlClient;
                          provider connection string='workstation id=ICookDB.mssql.somee.com;packet size=4096;user id=***;pwd=***;data source=ICookDB.mssql.somee.com;persist security info=False;initial catalog=ICookDB'"
        providerName="System.Data.EntityClient" />
</connectionStrings>

确保将<Path-to-your-SSDL><Path-to-your-MSL><Path-to-your-CSDL>替换为正确的位置。

您可以在此处获取有关如何创建正确连接字符串的更多信息: EntityConnection.ConnectionString Property

答案 1 :(得分:0)

由于您首先使用数据库,因此必须传递实体连接字符串。以下将从标准SQL连接字符串构建实体连接字符串:

public static string BuildEntityConnectionString(string connectionString, 
  Type contextType)
{
    string result = string.Empty;

    string prefix = contextType.Namespace
      .Replace(contextType.Assembly.GetName().Name, 
      "");

    if (prefix.Length > 0
        && prefix.StartsWith("."))
    {
        prefix = prefix.Substring(1, prefix.Length - 1);
    }

    if (prefix.Length > 1
        && !prefix.EndsWith("."))
    {
        prefix += ".";
    }

    EntityConnectionStringBuilder csBuilder = 
      new EntityConnectionStringBuilder();

    csBuilder.Provider = "System.Data.SqlClient";
    csBuilder.ProviderConnectionString = connectionString;
    csBuilder.Metadata = string.Format("res://{0}/{1}.csdl|"
                                        + "res://{0}/{1}.ssdl|"
                                        + "res://{0}/{1}.msl"
                                        , ContextType.Assembly.FullName
                                        , prefix + ContextType.Name);

    result =  csBuilder.ToString();

    return result;
}

然后在创建上下文时:

var connectionString = // however you get your connection string;
if (connectionString.IndexOf("metadata", StringComparison.OrdinalIgnoreCase) 
  == -1)
{
  connectionString = BuildEntityConnectionString(connectionString
    typeof(MyDbContext));
}

var myContext = new MyDbContext(connectionString);