EF迁移到SQL Azure

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)
            {

            }
        }
    }

    原文作者:灭蒙鸟
    原文地址: https://www.jianshu.com/p/330e41d3d09d
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞