OpenRobot.Framework.EntityFrameworkCore 2.0.0

dotnet add package OpenRobot.Framework.EntityFrameworkCore --version 2.0.0
                    
NuGet\Install-Package OpenRobot.Framework.EntityFrameworkCore -Version 2.0.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="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OpenRobot.Framework.EntityFrameworkCore" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="OpenRobot.Framework.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 OpenRobot.Framework.EntityFrameworkCore --version 2.0.0
                    
#r "nuget: OpenRobot.Framework.EntityFrameworkCore, 2.0.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@2.0.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=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=OpenRobot.Framework.EntityFrameworkCore&version=2.0.0
                    
Install as a Cake Tool

OpenRobot.Framework.EntityFrameworkCore

NuGet .NET

OpenRobot.Framework.EntityFrameworkCore 是 OpenRobot Web 框架的数据访问抽象核心层,提供 Entity Framework Core 的 DbContext 定义、仓储基类和种子数据基础设施。

主要特性

  • Entity Framework Core 8.0.8 - 基于最新的 EF Core
  • ABP 集成 - 与 ASP.NET Boilerplate 深度集成
  • 仓储模式 - 泛型仓储和自定义仓储支持
  • 多租户 - 支持多租户数据过滤
  • 种子数据 - 内置种子数据基础设施
  • Provider 无关 - 核心层不依赖具体数据库提供程序,由独立的 Provider 包提供数据库支持

安装

dotnet add package OpenRobot.Framework.EntityFrameworkCore

需要在项目中同时安装一个数据库提供程序包:

  • PostgreSQL: OpenRobot.Framework.EntityFrameworkCore.PostgreSQL
  • SQLite: OpenRobot.Framework.EntityFrameworkCore.Sqlite

数据库提供程序

框架采用 Provider 分离架构,支持多种数据库:

提供程序 NuGet 包 数据库
PostgreSQL OpenRobot.Framework.EntityFrameworkCore.PostgreSQL PostgreSQL 12+
SQLite OpenRobot.Framework.EntityFrameworkCore.Sqlite SQLite 3.x

选择提供程序

在应用入口模块(如 Web.Host)的 [DependsOn] 中引用对应的 Provider 模块:

// PostgreSQL
[DependsOn(
    typeof(WebFrameworkWebCoreModule),
    typeof(WebFrameworkPostgreSqlModule))]
public class WebFrameworkWebHostModule : AbpModule { }

// SQLite
[DependsOn(
    typeof(WebFrameworkWebCoreModule),
    typeof(WebFrameworkSqliteModule))]
public class WebFrameworkWebHostModule : AbpModule { }

快速开始

定义 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 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);
    }
}

数据库迁移

迁移命令请在对应的 Provider 项目目录下执行:

PostgreSQL 迁移

cd web/OpenRobot.Framework.EntityFrameworkCore.PostgreSQL
dotnet ef migrations add AddMyEntity -o Migrations
dotnet ef database update -s ../OpenRobot.Framework.Web.Host

SQLite 迁移

cd web/OpenRobot.Framework.EntityFrameworkCore.Sqlite
dotnet ef migrations add AddMyEntity -o Migrations
dotnet ef database update -s ../OpenRobot.Framework.Web.Host

依赖项

包名 版本 用途
Microsoft.EntityFrameworkCore 8.0.8 Entity Framework Core
Microsoft.EntityFrameworkCore.Design 8.0.8 设计时工具支持
Microsoft.EntityFrameworkCore.Tools 8.0.8 迁移工具

系统要求

  • .NET 8.0 或更高版本
  • 选择一个数据库提供程序包(PostgreSQL 或 SQLite)

在框架中的位置

OpenRobot.Framework.Web.Core
    └── OpenRobot.Framework.EntityFrameworkCore (抽象核心层) ✓
        ├── OpenRobot.Framework.Core ✓
        └── OpenRobot.Framework.EFCommon ✓

数据库提供程序实现:
OpenRobot.Framework.EntityFrameworkCore.PostgreSQL ✓
OpenRobot.Framework.EntityFrameworkCore.Sqlite ✓

核心功能

仓储模式

框架提供以下仓储接口:

  • 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();
}

目录结构

OpenRobot.Framework.EntityFrameworkCore/
├── EntityFrameworkCore/
│   ├── WebFrameworkDbContext.cs      # 主 DbContext
│   ├── AbpZeroDbMigrator.cs          # 数据库迁移器
│   ├── Repositories/                 # 自定义仓储(可选)
│   └── Seed/                         # 种子数据
│       ├── SeedHelper.cs
│       ├── Host/                     # 主机种子数据
│       └── Tenants/                  # 租户种子数据
└── Migrations/                       # 迁移占位目录

相关模块

  • OpenRobot.Framework.Core - 领域实体
  • OpenRobot.Framework.EFCommon - EF Core 共享配置
  • OpenRobot.Framework.Application - 应用程序服务层
  • OpenRobot.Framework.EntityFrameworkCore.PostgreSQL - PostgreSQL 提供程序
  • OpenRobot.Framework.EntityFrameworkCore.Sqlite - SQLite 提供程序

许可证

本项目采用 MIT 许可证。

作者

OpenRobot

Product 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.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on OpenRobot.Framework.EntityFrameworkCore:

Package Downloads
OpenRobot.Framework.Web.Core

OpenRobot.Framework.Web.Core 是 Web 层核心组件,提供身份验证、授权、API 控制器和 Swagger 配置

OpenRobot.Framework.EntityFrameworkCore.Sqlite

OpenRobot.Framework.EntityFrameworkCore.Sqlite 提供 SQLite 数据库的 EF Core 迁移和配置实现

OpenRobot.Framework.EntityFrameworkCore.PostgreSQL

OpenRobot.Framework.EntityFrameworkCore.PostgreSQL 提供 PostgreSQL 数据库的 EF Core 迁移和配置实现

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 137 6/5/2026
1.1.0 119 4/24/2026
1.0.1 113 4/21/2026