0

如何在域服务构造函数中更改 this.ObjectContext.Connection?

我使用多数据模型和多域服务。

    ...    
    <add name="PermissionEntities" connectionString="metadata=res://*/Entities.General.Permission.csdl|res://*/Entities.General.Permission.ssdl|res://*/Entities.General.Permission.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=10.10.10.10;initial catalog=Development;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="ProfileEntities" connectionString="metadata=res://*/Entities.General.Profile.csdl|res://*/Entities.General.Profile.ssdl|res://*/Entities.General.Profile.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=10.10.10.10;initial catalog=Development;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    ...

我想在运行时在域服务构造函数中更改 '.csdl' 、 '.ssdl' 、 '.msl' 。

4

1 回答 1

0

它通过CreateObjectContext域服务类中的覆盖来解决。

protected override Connection CreateObjectContext()
{  
     // Start out by creating the SQL Server connection string
     SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();

     // Set the properties for the data source. The IP address network address
     sqlBuilder.DataSource = @"10.10.10.10";

     // The name of the database on the server
     sqlBuilder.InitialCatalog = "Development";
     sqlBuilder.IntegratedSecurity = true;
     sqlBuilder.MultipleActiveResultSets = true;
     sqlBuilder.ApplicationName = "EntityFramework";

     // Now create the Entity Framework connection string
     EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();

     //Set the provider name.
     entityBuilder.Provider = "System.Data.SqlClient";

     // Set the provider-specific connection string.
     entityBuilder.ProviderConnectionString = sqlBuilder.ToString();

     // Set the Metadata location. 
     entityBuilder.Metadata = @"res://*/Entities.Permission.csdl|res://*/Entities.Permission.ssdl|res://*/Entities.Permission.msl";

     // Create and entity connection
     EntityConnection conn = new EntityConnection(entityBuilder.ToString());

     return new Connection(conn);    
}
于 2013-11-11T10:56:33.723 回答