Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore 26.8.13.2

dotnet add package Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore --version 26.8.13.2
                    
NuGet\Install-Package Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore -Version 26.8.13.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore" Version="26.8.13.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore" Version="26.8.13.2" />
                    
Directory.Packages.props
<PackageReference Include="Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore --version 26.8.13.2
                    
#r "nuget: Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore, 26.8.13.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore@26.8.13.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore&version=26.8.13.2
                    
Install as a Cake Addin
#tool nuget:?package=Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore&version=26.8.13.2
                    
Install as a Cake Tool

Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore

MAF 会话级存储 EF Core 模块。为 MAF Hosting 场景提供 AgentSessionStore 的持久化实现(EfCoreAgentSessionStore<TDbContext>),复用消息级模块 Galosys.Foundation.Agents.AI.EntityFrameworkCore 的会话行实体,将完整会话(含 StateBag)序列化持久化并在重启后恢复。

消息级聊天历史存储(ChatHistoryProvider)在 Galosys.Foundation.Agents.AI.EntityFrameworkCore 模块中。

安装

<PackageReference Include="Galosys.Foundation.Agents.AI.Hosting.EntityFrameworkCore" />

注册(覆盖内存存储)

AddEfCoreAgentSessionStore<TDbContext>() 以 RemoveAll + AddSingleton 覆盖 Hosting 默认的 InMemoryAgentSessionStore

using Microsoft.Extensions.DependencyInjection;

var builder = Host.CreateApplicationBuilder(args);

builder.Services.AddAgentsHostingCore();          // 默认内存 Agent 会话存储
builder.Services.AddDbContext<MyDbContext>(options => options.UseSqlite("Data Source=agents.db"));
builder.Services.AddEfCoreAgentSessionStore<MyDbContext>(); // 覆盖为 EF Core 持久化

HostedAgentBuilder 接入

using Microsoft.Agents.AI.Hosting;

var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddAgentsHostingCore();
builder.Services.AddDbContext<MyDbContext>(options => options.UseSqlite("Data Source=agents.db"));

var agentBuilder = builder.Services.AddAIAgent("assistant", "You are a helpful assistant.");
agentBuilder.WithEfCoreSessionStore<MyDbContext>();

多用户/多租户隔离(应用侧组合,模块不内置)

会话键默认 (agent, conversationId) 无主维度;需要隔离时用官方 IsolationKeyScopedAgentSessionStore 包装并自定 SessionIsolationKeyProvider(可读 ITenantContext / 用户标识):

public sealed class TenantSessionIsolationKeyProvider : SessionIsolationKeyProvider
{
    private readonly ITenantContext _tenants;
    public TenantSessionIsolationKeyProvider(ITenantContext tenants) => _tenants = tenants;

    public override ValueTask<string?> GetSessionIsolationKeyAsync(CancellationToken ct)
        => ValueTask.FromResult<string?>(_tenants.TenantId > 0 ? $"t{_tenants.TenantId}" : null);
}

var agentBuilder = builder.Services.AddAIAgent("assistant", "You are a helpful assistant.");
agentBuilder.WithEfCoreSessionStore<MyDbContext>(new TenantSessionIsolationKeyProvider(...));

隔离键非空时会话键改写为 {key}::{conversationId},不同租户/用户同名会话互不可见。

聊天历史持久化(消息级)

会话级存储持久化的是会话状态StateBag);若要让聊天消息落库(DevUI / OpenAI Responses / Conversations 端点亦生效,每次 Run 结束自动写入 ac_agent_message),需在 agent 上挂载 EfCoreChatHistoryProvider。用官方 AddAIAgent factory 重载手动绑定到 ChatHistoryProvider 槽位:

using Microsoft.Agents.AI;
using Microsoft.Agents.AI.Hosting;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;

var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddDbContext<MyDbContext>(options => options.UseSqlite("Data Source=agents.db"));

// 1. 注册消息级聊天历史存储(EfCoreChatHistoryProvider)
builder.Services.AddEfCoreChatHistoryProvider<MyDbContext>(options =>
{
    // 可选:消息完整性密钥 / 上下文压缩器 / 过滤
    // options.SigningKeyProvider = () => vault.GetKey();
});

// 2. 官方 factory 重载:挂载 EfCoreChatHistoryProvider 到 ChatHistoryProvider 槽位
builder.Services.AddAIAgent("assistant", (sp, key) =>
{
    var chatClient = sp.GetRequiredService<IChatClient>();
    return chatClient.AsAIAgent(new ChatClientAgentOptions
    {
        Name = key,
        ChatOptions = new ChatOptions { Instructions = "You are a helpful assistant." },
        ChatHistoryProvider = sp.GetRequiredService<EfCoreChatHistoryProvider<MyDbContext>>()
    });
})
.WithAITools(...); // 官方接线工具

对比:EfCoreAgentSessionStore(会话级)在 DevUI 端点链路不被自动触发,需手动调用;EfCoreChatHistoryProvider(消息级)挂在 agent 上后由 ChatClientAgent 自动持久化。

会话级 + 消息级 + 记忆/RAG + 中间件组合接线见基础模块 Galosys.Foundation.Agents.AI.Hosting README 的「注册并接线 Agent(官方 AddAIAgent factory 方式)」。

存储形态与限制

  • 客户端(单进程):Sqlite + 单例 DbContext,单写进程。
  • 服务端(多实例)DbContext Scoped,使用 SQL Server / Npgsql;Sqlite 不支持多实例并发写。
  • 会话级 EfCoreAgentSessionStore 与消息级 provider 均支持 IServiceScopeFactory 按操作解析,agent 生命周期 Scoped/Singleton 均安全,无 root-provider 异常。
  • 消息级与会话级写入时机不同(RunAsync 前后),单写进程下无冲突。

依赖

  • Galosys.Foundation.Agents.AI.Hosting — MAF Hosting(AgentSessionStore / SessionIsolationKeyProvider / IsolationKeyScopedAgentSessionStore
  • Galosys.Foundation.Agents.AI.EntityFrameworkCore — 消息级基础模块(复用 AgentSessionEntity
  • Galosys.Foundation.EntityFrameworkCore — Galosys DbContext<T> 基类
  • Microsoft.Agents.AI.Hosting — MAF Hosting(1.13.0-preview)
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
26.8.13.2 31 8/13/2026
26.8.13.1 58 8/13/2026
26.8.12.2 58 8/12/2026
26.8.12.1 67 8/12/2026
26.8.10.1 89 8/10/2026
26.8.5.1 88 8/5/2026