NXDO.Data.EFCore 1.0.0-preview.1

This is a prerelease version of NXDO.Data.EFCore.
dotnet add package NXDO.Data.EFCore --version 1.0.0-preview.1
                    
NuGet\Install-Package NXDO.Data.EFCore -Version 1.0.0-preview.1
                    
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="NXDO.Data.EFCore" Version="1.0.0-preview.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NXDO.Data.EFCore" Version="1.0.0-preview.1" />
                    
Directory.Packages.props
<PackageReference Include="NXDO.Data.EFCore" />
                    
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 NXDO.Data.EFCore --version 1.0.0-preview.1
                    
#r "nuget: NXDO.Data.EFCore, 1.0.0-preview.1"
                    
#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 NXDO.Data.EFCore@1.0.0-preview.1
                    
#: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=NXDO.Data.EFCore&version=1.0.0-preview.1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=NXDO.Data.EFCore&version=1.0.0-preview.1&prerelease
                    
Install as a Cake Tool

简述 NXDO.Data.EFCore

(EntityFramework Core) EF Core provider for NXDO.Data (ZhuBird)
得益于 NXDO.Data 配置的支持与解耦后Db工厂,可在 EFCore 中使用任意 Database 的 Ado.NET驱动
暂未实现: EntityFramework.Tools 的迁移功能,可使用替代方案 xx.Database.EnsureCreated() 建库建表

主要特点

  • 组件功能支持 linux
  • 环境支持 .NET 6.0,8.0,9.0
  • 可生成建表语句,支持模型对"NXDO实体"先行注册
  • EFCore执行原生sql,支持 NXDO.Data 中的 DB 参数类型
  • 支持 NXDO.Data 实体与普通 class 的实体

准备工作

  • 参见:NXDO.Data 的配置文件,并修改(如果需要修改)
  • 配置:NXDO.Data.Provider.json 放置到测试工程下(如果最新则复制到运行目录)
  • 学习:微软官网-EFCore学习资料

代码示例

  1. 实体定义 示例
using System.ComponentModel;    //DescriptionAttribute
using NXDO.Data;
using NXDO.Data.Attribute;

[Table("blog"), Description("测试blog")]    //建表时的表描述
public class Blog : NXDO.Data.Entity {
    [Description("我是主键")]               //建表时的字段描述
    [Field("blogid", Flag = FieldFlag.IsKey | FieldFlag.IsAutoValue)]
    [Relation(typeof(Post))]                //1:n,设置后 Posts 有效作用域
    public long BlogId { get; set; }

    [Field(FieldFlag.IsRequire)]
    [FieldDesign(Size = 200)]
    public string Name { get; set; }

    //非db字段
    public List<Post> Posts { get; } = new();
}

[Table("post")]
public class Post : NXDO.Data.Entity {
    [Field("postid", Flag = FieldFlag.IsKey | FieldFlag.IsAutoValue)]
    public long PostId { get; set; }

    [Field("title")]
    public string Title { get; set; }

    [Field("blogid"), Relation(typeof(Blog))]  //主表Blog,设置后 Blog 有效作用域
    public long BlogId { get; set; }

    //非db字段
    public Blog Blog { get; set; }
}
  1. 普通(EF千篇一律的写法) 示例
using System.Linq;
using Microsoft.EntityFrameworkCore; 

//DbContext,不是NXDO中的上下文环境,是EF的
public class BlogContext : DbContext {
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
        => optionsBuilder.UseDbWithProviderFactory();
    
    //声明属性,则模型自动注册实体
    public DbSet<Blog> Blogs { get; set; }
}


using var ctx = new BlogContext();        
ctx.Database.EnsureCreated();

var blog = new Blog() { Name="efcore" };
ctx.Blogs.Add(blog);                                            //一旦add,缺省主键值已自动生成

ctx.Add(new Post { BlogId = blog.BlogId, Title = "预览发布1" }); //明细缺省主键值,自动生成
ctx.Add(new Post { BlogId = blog.BlogId, Title = "预览发布2" }); //明细缺省主键值,自动生成
await ctx.SaveChangesAsync();

//明细查询包含主表(Blog属性)
var postSet = ctx.Set<Post>();
var post = postSet.Include(p => p.Blog).Where(p=>p.Title == "预览发布1").Select(p => p).ToArray();

3.1. Db环境中不声明 DbSet<实体> 示例

public class NoSetContext : DbContext {
    public ModelBuilder ModelBuilder { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
        optionsBuilder.UseDbWithProviderFactory();

        if (this.ModelBuilder != null)
            optionsBuilder.UseModel(this.ModelBuilder.FinalizeModel());
    }
}

//约定俗成,一般都在对应的 DbConventionSetBuilder 类下
var mb = NXDO.Data.EFCore.Metadata.Conventions.AnyDbConventionSetBuilder.CreateModelBuilder();
//var mb = NXDO.Data.EFCore.AnyEFCoreHelper.CreateModelBuilder(); //功能与上行code一样
mb.Entity<Blog>();
mb.Entity<Post>();

using var ctx = new NoSetContext() { ModelBuilder = mb };
var rs = from blog in ctx.Set<Blog>() select blog;

3.2. 不声明Db环境 示例

//DbContext的子类都不声明
using NXDO.Data.EFCore;
var mb = AnyEFCoreHelper.CreateModelBuilder();
mb.Entity<Blog>();
mb.Entity<Post>();

using var ctx = AnyEFCoreHelper.CreateDbContext(mb, configer => { configer.UseDbWithProviderFactory(); });
var postSet = ctx.Set<Post>();
var post = postSet.Include(p => p.Blog).Where(p => p.Title == "预览发布1").Select(p => p).ToArray();
  1. 扩展与其它 示例
//扩展注册实体
using NXDO.Data.Extension;
using var ctx = new NoSetContext();
ctx.EntryToModelBuilder(typeof(Blog), typeof(Post));

var postSet = ctx.Set<Post>();
var post = postSet.Include(p => p.Blog).Where(p => p.Title == "预览发布1").Select(p => p).ToArray();


//兼容 nxdo 的查询语句,可以平滑使用 EFCore
var nxParam = NXDO.Data.Factory.DbHelper.Parameter(1);
var rs = ctx.Set<Blog>().FromSqlRaw("select * from blog where blogid>{0}", nxParam).ToList();


//获取建表语句(配合 NXDO.Database.Startup)
//ctx的模型中,实体必须已经注册
ctx.Database.GenerateCreateScript();

注意事项

  • 数据库映射的 top 方法,内置无实现(sqlserver需注意),目前支持的是 limit (mysql/postgres)
  • 分页实际处理,还是建议使用 NXDO.Data,EFCore 中的分页性能还是差强人意(NXDO.Data.EFCore 后续会重写原生方法进行调整)。
  • 预览版中,方法注释上明确说明的:不要调用等字样,千万不要在生产环境中使用,后续可能被移除。
  • EFCore建库建表的方法,不建议在生产环境中使用,用EFCore生成sql语句,再配合 NXDO.Database.Startup 的参数设置,使用该类库的建库建表功能。
  • 关于原生的继承策略 TPH,TPT,TPC,NXDO中被全部忽略,仍然使用 NXDO.Data 实体的继承规则。
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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 is compatible.  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

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
1.0.0-preview.1 55 1/2/2026

首次发布。