Entity Framework
代理查询

代理查询

⚠️

该文档可能已过期。

代理查询(Agent Query)属于动态查询的特殊构建方式。

它可以将特定实体(Entity)的每个属性映射为对数据行(Row)的访问,可用于对 Key-Value 存储结构的便捷访问。


示例

  1. 定义 Key-Value 表实体 AppRegistryEntity
public class AppRegistryEntity : KeyValueEntity { }
public class ApplicationDbContext : DbContext
{
    public DbSet<AppRegistryEntity> AppRegistries { get; set; }
}
  1. 定义代理实体 AppRegistry
public class AppRegistry : KeyValueAgent<AppRegistryEntity>
{
    public virtual string Theme { get; set; } = "Default";
    public virtual string Color { get; set; }
 
    public virtual int Volume { get; set; }
    public virtual DateTime? LoginTime { get; set; }
 
    public virtual bool Lock { get; set; }
}
  1. 查询操作
using (var context = ApplicationDbContext.UseMySql())
using (var query = context.BeginAgentQuery(x => x.AppRegistries))
{
    var jerry = query.GetAgent<AppRegistry>("/User/Jerry");
    
    jerry.Theme = "Sky";
    jerry.Color = "brown";
    jerry.Volume = 10;
    jerry.LoginTime = now;
    jerry.Lock = true;
 
    context.SaveChanges();
}
  1. 执行结果
IdItemKeyValue
<Guid>/User/JerryThemeSky
<Guid>/User/JerryColorbrown
<Guid>/User/JerryVolume10
<Guid>/User/JerryLoginTime2023/1/1 15:00:00
<Guid>/User/JerryLockTrue