layout: docs-default
EF迁移到SQL Azure
如果需要连接上SQL Azure,需要配置 SqlAzureExecutionStrategy
,为了使用DbExecutionStrategy
,必须自定义一个DbConfiguration
。创建一个新类,填入下面的代码:
using System.Data.Entity;
using System.Data.Entity.SqlServer;
namespace IdentityServer3.Samples.EntityFramework
{
public class MyDbConfiguration: DbConfiguration
{
SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy());
}
}
有了自定义的 MyDbConfiguration
后,需要创建一个DbContext
的派生类,通过使用DbConfigurationType
特性来连接上面的代码
Custom ClientConfigurationDbContext
using System.Data.Entity;
using IdentityServer3.EntityFramework;
namespace IdentityServer3.Samples.EntityFramework
{
[DbConfigurationType(typeof(MyDbConfiguration))]
public class MyClientConfigurationDbContext : ClientConfigurationDbContext
{
public MyClientConfigurationDbContext()
{
}
public MyClientConfigurationDbContext(EntityFrameworkServiceOptions entityFrameworkConfig):
base(connectionString, schema)
{
}
public MyClientConfigurationDbContext(string connectionString, string schema):
base(connectionString, schema)
{
}
}
}
自定义ScopeConfigurationDbContext
using System.Data.Entity;
using IdentityServer3.EntityFramework;
namespace IdentityServer3.Samples.EntityFramework
{
[DbConfigurationType(typeof(MyDbConfiguration))]
public class MyScopeConfigurationDbContext : ScopeConfigurationDbContext
{
public MyScopeConfigurationDbContext()
{
}
public MyScopeConfigurationDbContext(EntityFrameworkServiceOptions entityFrameworkConfig):
base(entityFrameworkConfig.ConnectionString, entityFrameworkConfig.Schema)
{
}
public MyScopeConfigurationDbContext(string connectionString, string schema):
base(connectionString, schema)
{
}
}
}
自定义 OperationalDbContext
using System.Data.Entity;
using IdentityServer3.EntityFramework;
namespace IdentityServer3.Samples.EntityFramework
{
[DbConfigurationType(typeof(MyDbConfiguration))]
public class MyOperationalDbContext : OperationalDbContext
{
public MyOperationalDbContext()
{
}
public MyOperationalDbContext(EntityFrameworkServiceOptions entityFrameworkConfig):
base(entityFrameworkConfig.ConnectionString, entityFrameworkConfig.Schema)
{
}
public MyOperationalDbContext(string connectionString, string schema):
base(connectionString, schema)
{
}
}
}