A:增加数据库配置和使用
This commit is contained in:
1 parent
53d1dd2dbf
commit
f7660c49e8
86 files changed
+7401
-271
No files matched your search
@@ -0,0 +1,184 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Sharp.Data;
|
||||
|
||||
namespace Data.Test;
|
||||
|
||||
/// <summary>
|
||||
/// 快速开始 - 最简单的演示
|
||||
///
|
||||
/// 如果您是第一次使用 Sharp.Data,从这里开始!
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public class _00_QuickStart
|
||||
{
|
||||
/// <summary>
|
||||
/// 演示: 3 行代码开始使用 Sharp.Data
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public async Task QuickStart_ThreeLines()
|
||||
{
|
||||
// 第1行:创建构建器
|
||||
var builder = new DataContextBuilder<TestDbContext>();
|
||||
|
||||
// 第2行:配置数据库
|
||||
builder.UseInMemoryDatabase("QuickStartDemo");
|
||||
|
||||
// 第3行:构建并使用
|
||||
using var db = builder.Build();
|
||||
|
||||
// 完成!现在可以使用数据库了
|
||||
var productRepo = db.Repository<Product>();
|
||||
|
||||
// 添加产品
|
||||
await productRepo.AddAsync(new Product
|
||||
{
|
||||
Name = "iPhone 15",
|
||||
Price = 7999m,
|
||||
Stock = 100
|
||||
});
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
// 查询产品
|
||||
var products = await productRepo.GetAllAsync();
|
||||
|
||||
Assert.AreEqual(1, products.Count);
|
||||
Console.WriteLine($"✓ 成功添加并查询产品: {products[0].Name}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 演示: 启用审计和软删除
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public async Task QuickStart_WithAuditAndSoftDelete()
|
||||
{
|
||||
var builder = new DataContextBuilder<TestDbContext>();
|
||||
builder.UseInMemoryDatabase($"AuditDemo_{Guid.NewGuid()}")
|
||||
.UseAudit(true) // 启用审计
|
||||
.UseSoftDelete(true) // 启用软删除
|
||||
.UseCurrentUser("admin"); // 设置当前用户
|
||||
|
||||
using var db = builder.Build();
|
||||
var repo = db.Repository<Product>();
|
||||
|
||||
// 添加产品(自动记录创建信息)
|
||||
var product = new Product { Name = "测试产品", Price = 99m, Stock = 10 };
|
||||
await repo.AddAsync(product);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
Console.WriteLine($"✓ 产品已创建");
|
||||
Console.WriteLine($" CreatedBy: {product.CreatedBy}");
|
||||
Console.WriteLine($" CreatedAt: {product.CreatedAt:yyyy-MM-dd HH:mm:ss}");
|
||||
|
||||
// 软删除(不会真正删除)
|
||||
await repo.RemoveAsync(product.Id);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
Console.WriteLine($"\n✓ 产品已软删除");
|
||||
Console.WriteLine($" IsDeleted: {product.IsDeleted}");
|
||||
Console.WriteLine($" DeletedAt: {product.DeletedAt:yyyy-MM-dd HH:mm:ss}");
|
||||
|
||||
// 验证:正常查询看不到已删除的
|
||||
var found = await repo.GetByIdAsync(product.Id);
|
||||
Assert.IsNull(found);
|
||||
Console.WriteLine($"\n✓ 正常查询返回 null (已删除的数据被自动过滤)");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 演示: 分页查询
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public async Task QuickStart_Pagination()
|
||||
{
|
||||
var builder = new DataContextBuilder<TestDbContext>();
|
||||
builder.UseInMemoryDatabase($"PageDemo_{Guid.NewGuid()}");
|
||||
|
||||
using var db = builder.Build();
|
||||
var repo = db.Repository<Product>();
|
||||
|
||||
// 添加测试数据
|
||||
for (int i = 1; i <= 25; i++)
|
||||
{
|
||||
await repo.AddAsync(new Product
|
||||
{
|
||||
Name = $"产品{i}",
|
||||
Price = i * 10m,
|
||||
Stock = i
|
||||
});
|
||||
}
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
// 分页查询:第2页,每页10条
|
||||
var result = await repo.GetPagedAsync(2, 10);
|
||||
|
||||
Console.WriteLine($"分页查询结果:");
|
||||
Console.WriteLine($" 当前页: {result.PageIndex}");
|
||||
Console.WriteLine($" 每页数量: {result.PageSize}");
|
||||
Console.WriteLine($" 总记录数: {result.TotalCount}");
|
||||
Console.WriteLine($" 总页数: {result.TotalPages}");
|
||||
Console.WriteLine($" 当前页产品数: {result.Items.Count}");
|
||||
|
||||
Assert.AreEqual(2, result.PageIndex);
|
||||
Assert.AreEqual(25, result.TotalCount);
|
||||
Assert.AreEqual(3, result.TotalPages);
|
||||
Assert.AreEqual(10, result.Items.Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 演示: 使用自定义实体(不使用基类)
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public async Task QuickStart_CustomEntity()
|
||||
{
|
||||
var builder = new DataContextBuilder<TestDbContext>();
|
||||
builder.UseInMemoryDatabase($"CustomDemo_{Guid.NewGuid()}");
|
||||
|
||||
using var db = builder.Build();
|
||||
var repo = db.Repository<CustomProduct>();
|
||||
|
||||
// CustomProduct 不使用任何基类,完全自定义
|
||||
var product = new CustomProduct
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = "自定义产品",
|
||||
Price = 299.99m,
|
||||
CreatedDate = DateTime.UtcNow
|
||||
};
|
||||
|
||||
await repo.AddAsync(product);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
var found = await repo.GetByIdAsync(product.Id);
|
||||
Assert.IsNotNull(found);
|
||||
|
||||
Console.WriteLine($"✓ 自定义实体也能正常工作:");
|
||||
Console.WriteLine($" Name: {found.Name}");
|
||||
Console.WriteLine($" Price: {found.Price:C}");
|
||||
Console.WriteLine($" IsExpensive: {found.IsExpensive}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 演示: 直接使用 EF Core
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public async Task QuickStart_DirectEFCore()
|
||||
{
|
||||
var builder = new DataContextBuilder<TestDbContext>();
|
||||
builder.UseInMemoryDatabase($"DirectDemo_{Guid.NewGuid()}");
|
||||
|
||||
using var db = builder.Build();
|
||||
|
||||
// 可以直接访问 DbContext
|
||||
var context = db.Context;
|
||||
var products = context.Set<Product>();
|
||||
|
||||
// 使用 EF Core 原生 API
|
||||
products.Add(new Product { Name = "EF Core 产品", Price = 199m, Stock = 50 });
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// 使用 LINQ 查询
|
||||
var count = await products.CountAsync();
|
||||
|
||||
Assert.AreEqual(1, count);
|
||||
Console.WriteLine($"✓ 可以直接使用 EF Core,完全兼容!");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user