Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用EF Core和C“的数据库分片;_C#_Entity Framework Core_Sharding_Azure Elastic Sharding - Fatal编程技术网

C# 如何使用EF Core和C“的数据库分片;

C# 如何使用EF Core和C“的数据库分片;,c#,entity-framework-core,sharding,azure-elastic-sharding,C#,Entity Framework Core,Sharding,Azure Elastic Sharding,我目前正在将我6年的C#应用程序转换为.NETCoreV3和EFCore(也使用Blazor)。 除切分部分外,大部分功能正常。 我们的应用程序为每个客户端创建一个新的数据库。我们或多或少地使用了以下代码: 我现在正试图将其转换为EF Core,但仍停留在这一部分: // C'tor to deploy schema and migrations to a new shard protected internal TenantContext(string conn

我目前正在将我6年的C#应用程序转换为.NETCoreV3和EFCore(也使用Blazor)。 除切分部分外,大部分功能正常。
我们的应用程序为每个客户端创建一个新的数据库。我们或多或少地使用了以下代码:
我现在正试图将其转换为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;
        }
//C'tor部署架构并迁移到新的碎片
受保护的内部租户上下文(字符串连接字符串)
:base(SetInitializerForConnection(connectionString))
{
}
//在对基类c'tors的调用中只允许使用静态方法
私有静态字符串集InitializerForConnection(字符串连接字符串)
{
//我们需要进行存在性检查,以便部署模式
SetInitializer(新的CreateDatabaseIfNotExists());
返回连接字符串;
}
//用于数据相关路由的C'tor。此呼叫将打开路由到正确网络的已验证连接
//由碎片映射管理器进行碎片化。请注意,基类c'tor调用对于打开的连接将失败
//如果需要执行迁移并使用SQL凭据。这就是为什么会这样
//将c'tor分为DDR案例(此c'tor)和新碎片的内部c'tor。
public TenantContext(ShardMap ShardMap、T shardingKey、string connectionStr)
:base(CreateDDRConnection(shardMap、shardingKey、connectionStr),true/*contextOwnsConnection*/)
{
}
//在对基类c'tors的调用中只允许使用静态方法
私有静态DbConnection CreateDDRConnection(ShardMap ShardMap、T shardingKey、string connectionStr)
{
//没有初始化
Database.SetInitializer(null);
//要求shard map代理给定密钥的已验证连接
var conn=shardMap.OpenConnectionForKey(shardingKey、connectionStr、ConnectionOptions.Validate);
返回连接;
}
上面的代码无法编译,因为数据库对象在EF Core中不以这种方式存在。 我假设我可以使用
TenantContext.Database.EnsureCreated()简化它某处。但我不知道如何修改这些方法,删除哪些方法,更改哪些方法(以及如何更改)

当然,我一直在搜索使用sharding和EF Core的示例,但找不到它。 这里有没有人曾经在EF Core中这样做过,并且愿意分享


我特别想知道在
startup.cs中放置什么,以及如何在创建新客户端时创建新的分片/数据库

在EF.Core中,只需在配置中解析碎片。乙二醇

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

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

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


ASP.NET核心最佳实践可能要求在数据库上下文中注入
ITenantConfiguration
服务或类似服务。但模式是一样的。只需将注入的服务实例保存到DbContext字段,并在
OnConfiguring

中使用它。嘿,Paul,你是在问如何使用EF-Core自动迁移数据库吗?或者您在EF Core中创建分片连接时遇到了特定的错误或问题?嗨,Mark,我已经更新了我的帖子。我无法编译EF代码,因为EF Core中不存在数据库对象。请小心您想要的。经过数月的努力使EfCore正常工作,我现在回到Ef classic,它可以在.NETCORE上使用。生成的SQL中有太多的限制,在3.1中由于“哦,我们甚至没有尝试在客户端上求值”而变得更糟。谢谢@TomTom的警告。我同意。我们开始转换到.NET Core v3 en EF Core v3,期望v3意味着它相当成熟。但是如果你有点开箱即用,很难让它工作。我还花了数周时间与MS Identity集成,但无法使其正常工作。我们现在使用谷歌登录。切分也是这样,没有样本code@PaulMeems你解决问题了吗?我们仍然有类似的问题。感谢@david browne microsoft,在此过程中我还学习了.NET Core,这使我很难理解如何实现所有部分。您是否有一个完整的示例和/或一些文档链接?如果
GetTenantConnection()
是异步的,您应该怎么做?@david browne microsoft。我没有看到任何回应。您是否有完整的示例和/或文档链接?