当前位置: 首页 > news >正文

.net core API中使用LiteDB

LiteDB介绍

LiteDB 是一个小巧、快速和轻量级的 .NET NoSQL 嵌入式数据库。

  • 无服务器的 NoSQL 文档存储
  • 简单的 API,类似于 MongoDB
  • 100% 的 C# 代码支持 .NET 4.5 / NETStandard 1.3/2.0,以单个 DLL(不到 450KB)形式提供
  • 线程安全
  • 支持 ACID,完整的事务支持
  • 写入失败后的数据恢复(WAL 日志文件)
  • 使用 DES(AES)加密算法对数据文件进行加密
  • 使用属性或流畅的映射器 API 将 POCO 类映射为 BsonDocument
  • 存储文件和流数据(类似于 MongoDB 的 GridFS)
  • 单一数据文件存储(类似于 SQLite)
  • 对文档字段建立索引以实现快速搜索
  • 支持 LINQ 查询
  • 提供类似于 SQL 的命令来访问/转换数据
  • LiteDB Studio - 数据访问的精美用户界面
  • 开源且免费供所有人使用,包括商业用途

nuget安装

dotnet add package LiteDB --version 5.0.21

BsonRef定义关联关系

using LiteDB;namespace LiteDBAPI.Models
{public class Customer:BaseClass{[BsonField("customername")]public string Name { get; set; }}public class Order: BaseClass{[BsonField("price")]public double Price { get; set; }[BsonRef("customers")]public Customer Customer { get; set; }}public class BaseClass{[BsonId]public ObjectId ID { get; set; }}
}

封装helper

using LiteDB;
using LiteDBAPI.Models;
using System.Linq.Expressions;namespace LiteDBAPI
{public class LiteDBHelper{public readonly LiteDatabase db;public LiteDBHelper(){db = new LiteDatabase("Filename=database.db;Password=1234;Connection=shared");}public BsonValue insert<T>(T value,string collectionName){// Get a collection (or create, if doesn't exist)var col = db.GetCollection<T>(collectionName);// Insert new customer document (Id will be auto-incremented)return col.Insert(value); }public T getOrder<T, TRelated>(string orderid,string collectionName, Expression<Func<T, TRelated>> includeExpression) where T : BaseClass{ObjectId objectId = new ObjectId(orderid);var col = db.GetCollection<T>(collectionName);return col.Query().Where(x =>x.ID== objectId).Include(includeExpression).FirstOrDefault();}public bool update<T>(T instance,string collectionName){var col = db.GetCollection<T>(collectionName);return col.Update(instance);}}
}

controller使用

using LiteDB;
using LiteDBAPI.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;namespace LiteDBAPI.Controllers
{[Route("api/[controller]/[action]")][ApiController]public class LiteDBController : ControllerBase{private readonly LiteDBHelper db;public LiteDBController(LiteDBHelper db){this.db = db;}[HttpPost]public IActionResult insert([FromBody] string customername){Customer customer = new Customer() { Name = customername };// customerthis.db.insert(customer, "customers");// orderthis.db.insert(new Order() { Customer=customer }, "orders");return Ok(new { result="success"});}[HttpGet]public Order getOrder([FromQuery] string orderid){return db.getOrder<Order,Customer>(orderid,"orders",x=>x.Customer);}[HttpPut]public IActionResult updatePrice([FromQuery] string orderid){Order order = db.getOrder<Order, Customer>(orderid, "orders", x => x.Customer);order.Price =order.Price + 100;return Ok(db.update<Order>(order, "orders"));}}
}

加密

通过更改连接参数,添加password实现

官网

代码


http://www.mrgr.cn/news/46003.html

相关文章:

  • 学习threejs,添加户外光照光源
  • 回溯算法之图的作色问题详细解读(附带Java代码解读)
  • 有些类也需计划生育——单例模式
  • 深入探索与实践:抖音商品详情接口的高效调用与数据处理技术
  • BP神经网络的有标签分类Matlab代码
  • 构建高效数据处理桥梁:探索基于数据库驱动的自定义TypeHandler解决方案
  • Springboot——使用poi实现excel动态图片导入解析
  • 【习题】应用UX体验标准
  • Spring Boot 应用开发案例:在线书籍管理系统
  • 上海亚商投顾:创业板指大涨超17% 两市成交额超3.45万亿创
  • 【量化交易、股票预测】MASTER:以市场为导向的股票价格预测变压器
  • 宝兰德亮相2024国际信息通信展:数智创新,信创力量!
  • 哈夫曼树的定义?如何构造?
  • 是德(Keysight)N9030A、N9030B PXA信号分析仪
  • 【Linux】五种IO模型
  • 大模型的安全机制
  • 从零开始:用Python编写自己的简单游戏
  • 大话C++:第15篇 友元
  • 如何使用Python连接和操作MySQL数据库?请提供示例代码。
  • 产品推介——施密特触发器光耦KLH11LX产品系列