首页 > 其他分享 >EF Core 7.0 – JSON Column

EF Core 7.0 – JSON Column

时间:2023-07-14 17:00:12浏览次数:40  
标签:Core Column EF JSON Entity 7.0 Address public

@@EF Core 7 json 列

 

前言

SQL Server 支持 JSON, 以前写过一篇介绍 SQL Server – Work with JSON.  但 EF Core 一直没有支持。直到 EF Core 7.0 才支持。

EF Core 7 包含对 JSON 列的提供程序无关的支持,以及 SQL Server 的实现。此支持允许将从 .NET 类型生成的聚合映射到 JSON 文档。可以在聚合上使用普通的 LINQ 查询,这些查询将转换为钻取到 JSON 所需的相应查询构造。EF7 还支持更新和保存对 JSON 文档所做的更改。具体参考 https://learn.microsoft.com/zh-cn/ef/core/what-is-new/ef-core-7.0/whatsnew#json-columns

配置

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; } = "";
    public Address Address { get; set; } = null!;
}
 
public class Address
{
    public string Line1 { get; set; } = "";
    public string Line2 { get; set; } = "";
}

Address 是一个对象. 过往我们会把它配置成 Owned Entity, 属性 Line1, Line2 分别占据数据库 2 个 column. column name 是 Address_Line1, Address_Line2

这样是 ok 的, 但有时候我们希望它在数据库就一个 column, 然后用 JSON 格式保存资料. 反正数据库也支持 JSON 查询 (只是性能不容易优化), 一堆 column 毕竟很乱.

声明使用 JSON Column

它算是 Owned Entity 的扩展, 表面上依然是 Owned Entity, 但多了一个 ToJson() 声明

modelBuilder.Entity<Customer>().ToTable("Customer");
modelBuilder.Entity<Customer>().Property(e => e.Name).HasMaxLength(256);
modelBuilder.Entity<Customer>().OwnsOne(e => e.Address, addressBuilder =>
{
    addressBuilder.ToJson();
    addressBuilder.Property(e => e.Line1).HasMaxLength(256);
    addressBuilder.Property(e => e.Line2).HasMaxLength(256);
});

数据库长相

生成出来的数据长这样. Address 值就是 JSON 格式

 

查询的 LINQ 是完全一样的, EF Core 在背地里会转换成对应的 JSON Query

var customers = await db.Customers.Where(e => e.Address.Line1 == "lorem 1").ToListAsync();

Query

 

嵌套

如果有多层, 只需要在最上层设置 ToJson() 就可以了.

Update Value

像平常一样直接 update Entity 就可以了. EF Core 会转换成 JSON_MODIFY 语句去更新

 

Array OwnsMany

除了对象, Array 也是可以的. 使用 OwnsMany. 操作和 OwnsOne 一摸一样.

但是 List<string> 就没有办法哦. 而且 Array 是不支持过滤的.

modelBuilder.Entity<Customer>().OwnsMany(e => e.Addresses, addressesBuilder =>
{
    addressesBuilder.ToJson();
    addressesBuilder.Property(e => e.Line1).HasMaxLength(256);
    addressesBuilder.Property(e => e.Line2).HasMaxLength(256);
});

query

var customer = await db.Customers.Where(e => e.Addresses.Count() > 3).ToListAsync();

上面这句会直接报错...目前还不支持 

Limitation

Github – Support spatial types in JSON columns :https://github.com/dotnet/efcore/issues/28811

Github – Json: add support for collection of primitive types:https://github.com/dotnet/efcore/issues/28688

Github – Support LINQ to JSONPATH querying:https://github.com/dotnet/efcore/issues/28616

 

转 https://blog.csdn.net/sD7O95O/article/details/128586679

标签:Core,Column,EF,JSON,Entity,7.0,Address,public
From: https://www.cnblogs.com/wl-blog/p/17554430.html

相关文章

  • caused by: io.lettuce.core.RedisCommandExecutionException: NOAUTH Authentication
    1importcom.fasterxml.jackson.annotation.JsonAutoDetect;2importcom.fasterxml.jackson.annotation.PropertyAccessor;3importcom.fasterxml.jackson.databind.DeserializationFeature;4importcom.fasterxml.jackson.databind.ObjectMapper;5importcom.f......
  • QEMIU-基于CentOS7系统编译安装部署qemu 2.7.0 版本的环境
    【原文链接】QEMIU-基于CentOS系统编译安装部署QEMU环境备份原有的yum源配置cp/etc/yum.repos.d/CentOS-Base.repo/etc/yum.repos.d/CentOS-Base.repo.bak设置阿里云的yum源wget-O/etc/yum.repos.d/CentOS-Base.repohttp://mirrors.aliyun.com/repo/Centos-7.repo......
  • 【HMS Core】AR Engine中,运行时出现../../../../src/main/cpp/world_ar_application.h
    ​【问题描述】1、AREngine中,从官网下载的“NDK示例代码”,运行时出现../../../../src/main/cpp/world_ar_application.h:30:10:fatalerror:'glm.hpp'filenotfound,该如何解决?2、arengine4.0.0.5版本新增秒放特性。不需要扫描平面,就能创建锚点并放置虚拟物体,目前只支持创......
  • 【HMS Core】Health Kit 步数数据查询步骤咨询,血压/血氧的原子采样统计数据类型问题咨
    ​【问题描述】1、在进行步数查询---多日统计数据查询的时候,postman测试,发现了采样数据类型不匹配问题多日统计查询时,数据类型为 "com.huawei.continuous.steps.total"报错。反而数据类型为明细采样数据类型时“com.huawei.continuous.steps.delta”,正常返回。2、血压/血氧的......
  • ASP.Net Core Razor+AdminLTE 应用详解
     AdminLTE介绍 一个基于bootstrap的轻量级后台模板,这个前端界面个人感觉很清爽,对于一个大后端的我来说,可以减少较多的时间去承担前端的工作但又必须去独立去完成一个后台系统开发的任务,并且,文档还算比较齐全,对着demo可以完成一个基本的前端框架搭建了。大家如有更为好......
  • ASP.NET Core SignalR 系列(三)- JavaScript 客户端
    本章将和大家分享ASP.NETCoreSignalR中的JavaScript客户端。ASP.NETCoreSignalRJavaScript客户端库使开发人员能够调用服务器端SignalR中心代码。本文大部分内容摘自微软官网:https://learn.microsoft.com/zh-cn/aspnet/core/signalr/javascript-client?view=aspnetcore......
  • KubeFlow1.7.0部署
    KubeFlow是一个开源的项目,旨在为Kubernetes提供可组合、便携式、可扩展的机器学习技术栈。它最初是为了解决在Kubernetes上运行分布式机器学习任务所带来的挑战而创建的。Kubernetes本身是一个容器平台,但在近年来,越来越多的公司开始用它来运行各种工作负载,特别是机器学习任......
  • .Net Core Mvc Razor 组件
    目录前言调用页面index.cshtmlPartialUserViewComponent.csIUserServices.csPartialUserView.cshtml注意前言视图组件与分部视图类似,但它们的功能更加强大。视图组件不使用模型绑定,它们依赖于调用视图组件时传递的数据。本文是使用控制器和视图编写的,但视图组件适用于Razo......
  • Asp.Net Core 项目实战之权限管理系统使用AdminLTE搭建 -- 系列文章
    0Asp.NetCore项目实战之权限管理系统(0)无中生有1Asp.NetCore项目实战之权限管理系统(1)使用AdminLTE搭建前端2Asp.NetCore项目实战之权限管理系统(2)功能及实体设计3Asp.NetCore项目实战之权限管理系统(3)通过EntityFrameworkCore使用PostgreSQL4Asp.NetCore项目......
  • Authentication With ASP.NET Core Identity
    AuthenticationWithASP.NETCoreIdentity、PreparingtheAuthenticationEnvironmentinourProjectThefirstthing,wearegoingtodoisdisableunauthorizeduserstoaccesstheEmployeesaction.Todothat,wehavetoaddthe[Authorize]attributeonto......