Ef+Mysql在多字段路由下生成迁移脚本失败
配置忽略字段
builder.Ignore(x => x.SplitTableKey);
异常信息
Unable to create a 'DbContext' of type 'ShardingCore.MultiField.Demo.ApplicationDbContext'. The exception 'The property 'Player.SplitTableKey' could not be found. Ensure that the property exists and has been included in the model.' was thrown while attempting to create an instance. For the different patterns supported at design time
Demo地址 https://github.com/lohaaa/ShardingCore.MultiField.Demo
目前没有什么解决思路,请大佬帮忙看看,或有是否有最新的多字段分表demo提供参考。
@lohaaa 中午我抽空看看
默认会检查所有字段是否是分片又是自增,所以把这个检查去掉就好了,刚好你的字段不在模型里面
builder.Services.AddShardingDbContext<ApplicationDbContext>()
.UseRouteConfig(
options =>
options.AddShardingTableRoute<MultiFieldRoute>()
)
.UseConfig(options =>
{
//添加这一句
options.CheckShardingKeyValueGenerated = false;
options.AddDefaultDataSource("Default", builder.Configuration.GetConnectionString("Default"));
options.UseShardingQuery((s, optionsBuilder) =>
{
optionsBuilder.UseMySql(s, MySqlServerVersion.LatestSupportedServerVersion)
.UseLoggerFactory(efLogger);
});
options.UseShardingTransaction((connection, optionsBuilder) =>
{
optionsBuilder.UseMySql(connection, MySqlServerVersion.LatestSupportedServerVersion)
.UseLoggerFactory(efLogger);
});
options.UseShardingMigrationConfigure(optionsBuilder =>
optionsBuilder.ReplaceService<IMigrationsSqlGenerator, ShardingMySqlMigrationsSqlGenerator>());
}).AddShardingCore();
@xuejmnet 非常感谢,框架用起来很舒心。
@xuejmnet 还有一个问题需要请教,在多字段分表中我想如果表不存在自动创建表(动态追加增加多字段分表),如在MultiFieldRoute : AbstractShardingOperatorVirtualTableRoute<Player, string>中如何获取组合后的完整表后缀,如 Tabel_ACode_BCode 中的 ACode_BCode 部分,以及在何时执行创建表方法。
表不存在就创建参考源码samples里面autoCreateIfPresent例子
第二个问题路路由怎么写参考https://github.com/xuejmnet/TwoPropertyDemo 完整后缀只能在GetRouteToFilter返回的Func入参里面获取现有的
@xuejmnet 当我只在查询条件中放入额外属性的话调用链过程中不会进入GetRouteToFilter方法,这种情况如何获取到。
await dbContext.Players.Where(x => x.AppCode == "game" && x.GroupCode == "group")
public virtual Func<string, bool> GetRouteFilter( object shardingKey, ShardingOperatorEnum shardingOperator, string shardingPropertyName) { return this.EntityMetadata.IsMainShardingTableKey(shardingPropertyName) ? this.GetRouteToFilter(this.CompareValueToKey(shardingKey), shardingOperator) : this.GetExtraRouteFilter(shardingKey, shardingOperator, shardingPropertyName); }
demo地址:https://github.com/lohaaa/ShardingCore.MultiField.Demo
https://www.cnblogs.com/xuejiaming/p/15728340.html 可以看下这篇文章
https://github.com/xuejmnet/TwoPropertyDemo 这个demo你看了吗 https://github.com/xuejmnet/TwoPropertyDemo/blob/main/TwoPropertyDemo/Sharding.cs 这个里面写了对应的双字段路由
https://www.cnblogs.com/xuejiaming/p/15728340.html 可以看下这篇文章
https://github.com/xuejmnet/TwoPropertyDemo 这个demo你看了吗 https://github.com/xuejmnet/TwoPropertyDemo/blob/main/TwoPropertyDemo/Sharding.cs 这个里面写了对应的双字段路由
demo和文章在之前我都看过了,目前不理解的地方可能在,当两个分片键都是动态的时候如何获取完整的后缀,然后使用autoCreateIfPresent里面的方式动态创建表,我周末再研究一下吧,感谢解答。
@lohaaa 你想错了,查询因为走的两个字段所以取不到全名称但是你如果想要不存在创建应该是插入的时候,所以插入的时候会走GetRouteToFilter
@lohaaa 你想错了,查询因为走的两个字段所以取不到全名称但是你如果想要不存在创建应该是插入的时候,所以插入的时候会走GetRouteToFilter
是的,我进入误区了,茅塞顿开,仔细看源码的话在 if isQuery我就应该找到答案,还是大意了,非常感谢作者大大的解答。
在这边处理的RouteWithValue
我之前也说错了应该是对象会走RouteWithValue而不是GetRouteToFilter
我之前也说错了应该是对象会走
RouteWithValue而不是GetRouteToFilter
目前已经完善好,跑通了。