英文:
EF searching by fields from dynamic table
问题
我有3个共享公共字段的表格
class T1 : IHasUserID
{
public string Id { get; set; }
public string UserId { get; set; }
//其他属性
}
class T2 : IHasUserID
{
public string Id { get; set; }
public string UserId { get; set; }
//其他属性
}
class T3 : IHasUserID
{
public string Id { get; set; }
public string UserId { get; set; }
//其他属性
}
实体类型在运行时定义,按Id查找很容易,因为它是主键,所以我可以这样写:
var res = (IHasUserId)dbContext.FindAsync(ctx.TableType, id)
但如何按UserId搜索呢?
英文:
I have 3 tables that share common fields
class T1:IHasUserID
{
public string Id {get;set;}
public string UserId {get;set;}
//other properties
}
class T2:IHasUserID
{
public string Id {get;set;}
public string UserId {get;set;}
//other properties
}
class T3:IHasUserID
{
public string Id {get;set;}
public string UserId {get;set;}
//other properties
}
type of entity is defined at runtime
finding by Id is easy as it's PK so I can write like this:
var res = (IHasUserId) dbContext.FindAsync(ctx.TableType, id)
but how do I search by UserId
答案1
得分: 0
不确定这是否是您想要做的,但通常我会像这样按UserID搜索:
var tableRow = ctx.TableType.Where(tt => tt.UserId == id).FirstOrDefault();
另外,我会在 IHasUserID
中添加ID和UserId。这是因为您的T1、T2和T3表是IHasUserID
的扩展,应该包含在依赖于它的所有表之间共同的变量。
像这样:
class IHasUserID
{
public string Id { get; set; }
public string UserId { get; set; }
}
class T1 : IHasUserID
{
//其他属性
}
class T2 : IHasUserID
{
//其他属性
}
class T3 : IHasUserID
{
//其他属性
}
英文:
Unsure if this is what you're trying to do, but to search by UserID i usually do it like this
var tableRow = ctx.TableType.Where(tt=> tt.UserId == id).FirstOrDefault();
Also I would add both ID and UserId in IHasUserID. This is because your T1, T2, T3 tables are extensions to IHasUserID, and should hold the variables that are common between all tables depending on it.
So like this:
class IHasUserID
{
public string Id {get;set;}
public string UserId {get;set;}
}
class T1:IHasUserID
{
//other properties
}
class T2:IHasUserID
{
//other properties
}
class T3:IHasUserID
{
//other properties
}
答案2
得分: -1
感谢所有评论的方式,我已经创建了通用方法:
private async ValueTask<int?> GetId<TTable>(string userId) where TTable : IHasUserID
{
TSagaTable res = await dbContext.Set<TSagaTable>().FirstAsync(_ => _.UserId == userId);
return res.Id;
}
然后像这样运行它:
public async ValueTask<int?> GetIdByUser(string kind, string userId)
{
Type tableType = kindProvider.GetAll().First(x => x.Kind == kind).JsonTableType;
MethodInfo method = typeof(IdResolver).GetMethod(nameof(GetId), BindingFlags.NonPublic | BindingFlags.Instance)!;
MethodInfo generic = method.MakeGenericMethod(tableType);
return await (ValueTask<int?>)generic.Invoke(this, new object[] { userId });
}
英文:
thanks to all comments the way I did it I've created generic method
private async ValueTask<int?> GetId<TTable>(string userId) where TTable: IHasUserID
{
TSagaTable res = await dbContext.Set<TSagaTable>().FirstAsync(_ => _.UserId == userId );
return res.Id
}
and run it like so
public async ValueTask<int?> GetIdByUser(string kind, string userId)
{
Type tableType = kindProvider.GetAll().First(x => x.Kind== kind).JsonTableType;
MethodInfo method = typeof(IdResolver).GetMethod(nameof(GetId),BindingFlags.NonPublic | BindingFlags.Instance)!;
MethodInfo generic = method.MakeGenericMethod(tableType);
return await (ValueTask<int?>) generic.Invoke(this,new object []{ userId});
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论