OpenRobot.Framework.EntityFrameworkCore
1.1.0
dotnet add package OpenRobot.Framework.EntityFrameworkCore --version 1.1.0
NuGet\Install-Package OpenRobot.Framework.EntityFrameworkCore -Version 1.1.0
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="OpenRobot.Framework.EntityFrameworkCore" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OpenRobot.Framework.EntityFrameworkCore" Version="1.1.0" />
<PackageReference Include="OpenRobot.Framework.EntityFrameworkCore" />
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 OpenRobot.Framework.EntityFrameworkCore --version 1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: OpenRobot.Framework.EntityFrameworkCore, 1.1.0"
#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 OpenRobot.Framework.EntityFrameworkCore@1.1.0
#: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=OpenRobot.Framework.EntityFrameworkCore&version=1.1.0
#tool nuget:?package=OpenRobot.Framework.EntityFrameworkCore&version=1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
OpenRobot.Framework.EntityFrameworkCore
OpenRobot.Framework.EntityFrameworkCore 是 OpenRobot Web 框架的数据访问层,提供 Entity Framework Core 的完整实现、数据库迁移、仓储模式和多种数据库提供程序支持。
主要特性
- Entity Framework Core 8.0.8 - 基于最新的 EF Core
- 多数据库支持 - 支持 PostgreSQL、SQLite 等多种数据库
- ABP 集成 - 与 ASP.NET Boilerplate 深度集成
- 仓储模式 - 泛型仓储和自定义仓储支持
- 数据库迁移 - 完整的 Code First 迁移支持
- 多租户 - 支持多租户数据过滤
- 自定义配置 - 灵活的 DbContext 配置
安装
dotnet add package OpenRobot.Framework.EntityFrameworkCore
或在 Visual Studio 的 NuGet 包管理器控制台中运行:
Install-Package OpenRobot.Framework.EntityFrameworkCore
快速开始
定义 DbContext
using Abp.EntityFrameworkCore;
using OpenRobot.Framework.Core.Authorization.Users;
namespace OpenRobot.Framework.EntityFrameworkCore
{
public class WebFrameworkDbContext : AbpZeroDbContext<Tenant, Role, User, WebFrameworkDbContext>
{
public WebFrameworkDbContext(DbContextOptions<WebFrameworkDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// 自定义实体配置
modelBuilder.Entity<MyEntity>(b =>
{
b.ToTable("MyEntities");
b.Property(x => x.Name).IsRequired().HasMaxLength(256);
});
}
}
}
配置数据库连接
using Microsoft.EntityFrameworkCore;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<WebFrameworkDbContext>(options =>
options.UseNpgsql(
Configuration.GetConnectionString("WebFramework"),
npgsqlOptions => npgsqlOptions.UseQuerySplittingBehavior(
QuerySplittingBehavior.SplitQuery)
)
);
}
}
使用仓储
using Abp.Domain.Repositories;
public class MyEntityAppService : ApplicationService
{
private readonly IRepository<MyEntity, long> _repository;
public MyEntityAppService(IRepository<MyEntity, long> repository)
{
_repository = repository;
}
public async Task<MyDto> CreateAsync(CreateMyDto input)
{
var entity = ObjectMapper.Map<MyEntity>(input);
await _repository.InsertAsync(entity);
return ObjectMapper.Map<MyDto>(entity);
}
public async Task<List<MyDto>> GetAllAsync()
{
var entities = await _repository.GetAllListAsync();
return ObjectMapper.Map<List<MyDto>>(entities);
}
}
数据库迁移
创建迁移
cd web/OpenRobot.Framework.EntityFrameworkCore
dotnet ef migrations add AddMyEntity
更新数据库
dotnet ef database update
生成 SQL 脚本
dotnet ef migrations script
依赖项
| 包名 | 版本 | 用途 |
|---|---|---|
| Microsoft.EntityFrameworkCore | 8.0.8 | Entity Framework Core |
| Microsoft.EntityFrameworkCore.Sqlite | 8.0.8 | SQLite 数据库提供程序 |
| Microsoft.EntityFrameworkCore.Design | 8.0.8 | 设计时工具支持 |
| Microsoft.EntityFrameworkCore.Tools | 8.0.8 | 迁移工具 |
| Npgsql.EntityFrameworkCore.PostgreSQL | 8.0.4 | PostgreSQL 数据库提供程序 |
系统要求
- .NET 8.0 或更高版本
- PostgreSQL 12+(使用 PostgreSQL 时)
- SQLite 3.x(使用 SQLite 时)
在框架中的位置
OpenRobot.Framework.Web.Core
└── OpenRobot.Framework.EntityFrameworkCore ✓
├── OpenRobot.Framework.Core ✓
└── OpenRobot.Framework.EFCommon ✓
数据库配置
连接字符串配置 (appsettings.json)
{
"ConnectionStrings": {
"WebFramework": "Host=localhost;Port=5432;Database=webframework;Username=postgres;Password=your_password",
"SQLite": "Data Source=webframework.db"
}
}
数据库提供程序选择
框架支持以下数据库提供程序:
- PostgreSQL (Npgsql.EntityFrameworkCore.PostgreSQL) - 生产环境推荐
- SQLite (Microsoft.EntityFrameworkCore.Sqlite) - 开发和小型应用
- SQL Server (Microsoft.EntityFrameworkCore.SqlServer) - 需要额外添加包
核心功能
仓储模式
框架提供以下仓储接口:
IRepository<TEntity, TPrimaryKey>- 主键仓储IRepository<TEntity>- 简化仓储(默认 int 主键)
多租户支持
// 自动过滤当前租户的数据
var entities = await _repository.GetAllListAsync();
// 禁用租户过滤
using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.MayHaveTenant))
{
var allEntities = await _repository.GetAllListAsync();
}
软删除
// 自动过滤已删除的实体
var activeEntities = await _repository.GetAllListAsync();
// 包含已删除的实体
using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.SoftDelete))
{
var allEntities = await _repository.GetAllListAsync();
}
自定义配置
IDbContextConfigurer
using Abp.EntityFrameworkCore;
public class CustomDbConfigurer : IDbContextConfigurer
{
public void Configure(ISetupDbContext setupContext)
{
setupContext.DbContextOptions.UseNpgsql(
setupContext.ConnectionString,
options =>
{
options.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery);
options.EnableRetryOnFailure(3);
}
);
}
}
目录结构
OpenRobot.Framework.EntityFrameworkCore/
├── EntityFrameworkCore/
│ ├── WebFrameworkDbContext.cs # 主 DbContext
│ └── DbSnapshots/ # 迁移快照
├── Migrations/ # 数据库迁移
└── Repositories/ # 自定义仓储(可选)
相关模块
OpenRobot.Framework.Core- 领域实体OpenRobot.Framework.EFCommon- EF Core 共享配置OpenRobot.Framework.Application- 应用程序服务层
许可证
本项目采用 MIT 许可证。
作者
OpenRobot
反馈与贡献
欢迎通过以下方式提供反馈:
- 提交 Issue
- 发起 Pull Request
- 联系维护者
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. 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.
-
net8.0
- Microsoft.EntityFrameworkCore (>= 8.0.8)
- Microsoft.EntityFrameworkCore.Sqlite (>= 8.0.8)
- Npgsql.EntityFrameworkCore.PostgreSQL (>= 8.0.4)
- OpenRobot.Framework.Common (>= 1.1.0)
- OpenRobot.Framework.Core (>= 1.1.0)
- OpenRobot.Framework.EFCommon (>= 1.1.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on OpenRobot.Framework.EntityFrameworkCore:
| Package | Downloads |
|---|---|
|
OpenRobot.Framework.Web.Core
OpenRobot.Framework.Web.Core 是 Web 层核心组件,提供身份验证、授权、API 控制器和 Swagger 配置 |
GitHub repositories
This package is not used by any popular GitHub repositories.