如何在 EF Core 和 C# 中使用数据库分片”

Pau*_*ems 13 c# sharding entity-framework-core azure-elastic-sharding

我目前正在将我 6 岁的 C# 应用程序转换为 .NET Core v3 和 EF Core(并且还使用 Blazor)。除了分片部分外,大部分都在工作。
我们的应用程序为每个客户端创建一个新数据库。我们或多或少地使用此代码:https : //docs.microsoft.com/en-us/azure/sql-database/sql-database-elastic-scale-use-entity-framework-applications-visual-studio
I '现在正在尝试将其转换为 EF Core,但在这部分卡住了:

        // C'tor to deploy schema and migrations to a new shard
        protected internal TenantContext(string connectionString)
            : base(SetInitializerForConnection(connectionString))
        {
        }

        // Only static methods are allowed in calls into base class c'tors
        private static string SetInitializerForConnection(string connnectionString)
        {
            // We want existence checks so that the schema can get deployed
            Database.SetInitializer<TenantContext<T>>(new CreateDatabaseIfNotExists<TenantContext<T>>());
            return connnectionString;
        }

        // C'tor for data dependent routing. This call will open a validated connection routed to the proper
        // shard by the shard map manager. Note that the base class c'tor call will fail for an open connection
        // if migrations need to be done and SQL credentials are used. This is the reason for the 
        // separation of c'tors into the DDR case (this c'tor) and the internal c'tor for new shards.
        public TenantContext(ShardMap shardMap, T shardingKey, string connectionStr)
            : base(CreateDDRConnection(shardMap, shardingKey, connectionStr), true /* contextOwnsConnection */)
        {
        }

        // Only static methods are allowed in calls into base class c'tors
        private static DbConnection CreateDDRConnection(ShardMap shardMap, T shardingKey, string connectionStr)
        {
            // No initialization
            Database.SetInitializer<TenantContext<T>>(null);

            // Ask shard map to broker a validated connection for the given key
            var conn = shardMap.OpenConnectionForKey<T>(shardingKey, connectionStr, ConnectionOptions.Validate);
            return conn;
        }
Run Code Online (Sandbox Code Playgroud)

上面的代码无法编译,因为 EF Core 中不以这种方式存在 Database 对象。我想我可以在TenantContext.Database.EnsureCreated();某个地方使用它来简化它。但是我不知道如何修改方法,删除哪些,更改哪些(以及如何更改)。

当然,我一直在寻找使用分片和 EF Core 的示例,但找不到。这里有没有人以前在 EF Core 中做过这个并且愿意分享?

我特别在寻找在startup.cs创建新客户端时要放入的内容以及如何创建新的分片/数据库。

Dav*_*oft 7

在 EF.Core 中,只需解析 OnConfiguring 中的分片。例如

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    var con = GetTenantConnection(this.tenantName);

    optionsBuilder.UseSqlServer(con,o => o.UseRelationalNulls());

    base.OnConfiguring(optionsBuilder);
}
Run Code Online (Sandbox Code Playgroud)

请注意,如果您有一个返回打开的DbConnections的服务或工厂,那么您需要在 DbContext.Dispose() 中 Close()/Dispose() 它们。如果您获得连接字符串或关闭的连接,则 DbContext 将负责关闭连接。

ASP.NET Core 最佳实践可能要求在ITenantConfigurationDbContext 中注入服务或类似内容。但是模式是一样的。只需将注入的服务实例保存到 DbContext 字段并在OnConfiguring.

  • 感谢@david-browne-microsoft,我也在这个过程中学习.NET Core,这让我很难理解如何实现所有部分。您有完整的工作示例和/或一些文档链接吗? (2认同)