System.Data.EntityClient无法通过代码识别,但由web.config识别

时间:2011-03-04 12:28:24

标签: silverlight-4.0 entity-framework-4 connection-string

当我只是运行项目时,数据从服务器获取没有问题,但是需要连接字符串在代码中。

根据网上的资料我做了以下,但是无法解决错误。

  

错误

     

在配置中找不到指定的商店提供商,或者无效。

的DomainService

public class DomainService1 : LinqToEntitiesDomainService<EMPLOYEEEntities>
    {

        public override void Initialize(DomainServiceContext context)
        {

            EntityConnectionStringBuilder builder = new EntityConnectionStringBuilder();
            builder.Provider = "System.Data.EntityClient";
            builder.ProviderConnectionString = @"Data Source=A-63A9D4D7E7834\THIRD;Initial Catalog=EMPLOYEE;Integrated Security=True;";
            builder.Metadata = string.Format(@"res://*/{0}.csdl|
                    res://*/{0}.ssdl|
                    res://*/{0}.msl", "Model1");

            this.ObjectContext.Connection.ConnectionString = builder.ConnectionString;
            base.Initialize(context);

        }

的web.config

<connectionStrings>
    <add name="EMPLOYEEEntities"
         connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=A-63A9D4D7E7834\THIRD;Initial Catalog=EMPLOYEE;Integrated Security=True;MultipleActiveResultSets=True&quot;" 
         providerName="System.Data.EntityClient" />
  </connectionStrings>

1 个答案:

答案 0 :(得分:0)

“System.Data.EntityClient”是许多名称空间/库的组合,因此无法识别。

正确的方法是,

public IQueryable<Table1> GetTable1()
{



    // Specify the provider name, server and database.
    string providerName = "System.Data.SqlClient";
    string serverName = @"A-63A9D4D7E7834\THIRD";
    string databaseName = "EMPLOYEE";

    // Initialize the connection string builder for the
    // underlying provider.
    SqlConnectionStringBuilder sqlBuilder =
        new SqlConnectionStringBuilder();

    // Set the properties for the data source.
    sqlBuilder.DataSource = serverName;
    sqlBuilder.InitialCatalog = databaseName;
    sqlBuilder.IntegratedSecurity = true;
    sqlBuilder.MultipleActiveResultSets=True;

    // Build the SqlConnection connection string.
    string providerString = sqlBuilder.ToString();

    // Initialize the EntityConnectionStringBuilder.
    EntityConnectionStringBuilder entityBuilder =
        new EntityConnectionStringBuilder();

    //Set the provider name.
    entityBuilder.Provider = providerName;

    // Set the provider-specific connection string.
    entityBuilder.ProviderConnectionString = providerString;


    // <add name="EMPLOYEEEntities" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=A-63A9D4D7E7834\THIRD;Initial Catalog=EMPLOYEE;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />



    // Set the Metadata location.
    entityBuilder.Metadata = @"res://*/Models.Model1.csdl|
                    res://*/Models.Model1.ssdl|
                    res://*/Models.Model1.msl";
    Console.WriteLine(entityBuilder.ToString());

   // EntityConnection conn =                 new EntityConnection(entityBuilder.ToString());
    /*
    using (EntityConnection conn =
        new EntityConnection(entityBuilder.ToString()))
    {
        conn.Open();
        Response.Write("this is a web application");
        Console.WriteLine("Just testing the connection.");
        conn.Close();
    }*/

    this.ObjectContext.Connection.ConnectionString = entityBuilder.ToString();
   // this.ObjectContext.Connection.Open();
  // this.ObjectContext.Connection.BeginTransaction();

   //  this.ObjectContext.Table1.OrderBy(q => q.ID);

    IQueryable<Table1> results = this.ObjectContext.Table1.OrderBy(q => q.ID);

  //  this.ObjectContext.Connection.Close();

    return results;
}