feat(新增): EfCore的CURD

This commit is contained in:
bunny 2024-08-09 09:26:32 +08:00
parent d8408a6792
commit 1c2ce54804
13 changed files with 547 additions and 7 deletions

View File

@ -7,7 +7,7 @@ public class EfCoreContext : DbContext
{
public EfCoreContext()
{
DbPath = @"D:\Project\web\Bunny-Cli\template\CSharp\CSharp-Single-EFCore\Bunny.WebApi\Database\bunny.db";
DbPath = @"D:\MyFolder\Bunny\Bunny-cli\Template\CSharp\CSharp-Single-EFCore\Bunny.WebApi\Database\bunny.db";
// var dataBaseConnection = AppSettings.GetAppConfig<string>("DataBase:DataBaseConnection");
// DbPath = dataBaseConnection;
}

View File

@ -0,0 +1,173 @@
// <auto-generated />
using System;
using Bunny.Common.EFCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace Bunny.Common.Migrations
{
[DbContext(typeof(EfCoreContext))]
[Migration("20240809011221_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.7");
modelBuilder.Entity("Bunny.Dao.Models.System.Blog", b =>
{
b.Property<string>("Id")
.HasColumnType("TEXT");
b.Property<DateTime>("CreateTime")
.HasColumnType("TEXT");
b.Property<long>("CreateUserId")
.HasColumnType("INTEGER");
b.Property<DateTime>("UpdateTime")
.HasColumnType("TEXT");
b.Property<long>("UpdateUserId")
.HasColumnType("INTEGER");
b.Property<string>("Url")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Blogs");
});
modelBuilder.Entity("Bunny.Dao.Models.System.Post", b =>
{
b.Property<string>("Id")
.HasColumnType("TEXT");
b.Property<int>("BlogId")
.HasColumnType("INTEGER");
b.Property<string>("BlogId1")
.HasColumnType("TEXT");
b.Property<string>("Content")
.HasColumnType("TEXT");
b.Property<DateTime>("CreateTime")
.HasColumnType("TEXT");
b.Property<long>("CreateUserId")
.HasColumnType("INTEGER");
b.Property<int>("PostId")
.HasColumnType("INTEGER");
b.Property<string>("Title")
.HasColumnType("TEXT");
b.Property<DateTime>("UpdateTime")
.HasColumnType("TEXT");
b.Property<long>("UpdateUserId")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.HasIndex("BlogId1");
b.ToTable("Posts");
});
modelBuilder.Entity("Bunny.Dao.Models.System.Product", b =>
{
b.Property<string>("Id")
.HasColumnType("TEXT");
b.Property<DateTime>("CreateTime")
.HasColumnType("TEXT");
b.Property<long>("CreateUserId")
.HasColumnType("INTEGER");
b.Property<string>("CustomerId")
.HasColumnType("TEXT");
b.Property<string>("CustomerName")
.HasColumnType("TEXT");
b.Property<string>("PackageType")
.HasColumnType("TEXT");
b.Property<DateTime>("UpdateTime")
.HasColumnType("TEXT");
b.Property<long>("UpdateUserId")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.ToTable("Products");
});
modelBuilder.Entity("Bunny.Dao.Models.System.User", b =>
{
b.Property<string>("Id")
.HasColumnType("TEXT");
b.Property<string>("CompanyCode")
.HasColumnType("TEXT");
b.Property<DateTime>("CreateTime")
.HasColumnType("TEXT");
b.Property<long>("CreateUserId")
.HasColumnType("INTEGER");
b.Property<string>("DeptCode")
.HasColumnType("TEXT");
b.Property<string>("Email")
.HasColumnType("TEXT");
b.Property<string>("Password")
.HasColumnType("TEXT");
b.Property<string>("Phone")
.HasColumnType("TEXT");
b.Property<string>("QrCode")
.HasColumnType("TEXT");
b.Property<DateTime>("UpdateTime")
.HasColumnType("TEXT");
b.Property<long>("UpdateUserId")
.HasColumnType("INTEGER");
b.Property<string>("UserName")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Users");
});
modelBuilder.Entity("Bunny.Dao.Models.System.Post", b =>
{
b.HasOne("Bunny.Dao.Models.System.Blog", "Blog")
.WithMany()
.HasForeignKey("BlogId1");
b.Navigation("Blog");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,117 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Bunny.Common.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Blogs",
columns: table => new
{
Id = table.Column<string>(type: "TEXT", nullable: false),
Url = table.Column<string>(type: "TEXT", nullable: false),
CreateTime = table.Column<DateTime>(type: "TEXT", nullable: false),
UpdateTime = table.Column<DateTime>(type: "TEXT", nullable: false),
CreateUserId = table.Column<long>(type: "INTEGER", nullable: false),
UpdateUserId = table.Column<long>(type: "INTEGER", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Blogs", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Products",
columns: table => new
{
Id = table.Column<string>(type: "TEXT", nullable: false),
CustomerId = table.Column<string>(type: "TEXT", nullable: true),
CustomerName = table.Column<string>(type: "TEXT", nullable: true),
PackageType = table.Column<string>(type: "TEXT", nullable: true),
CreateTime = table.Column<DateTime>(type: "TEXT", nullable: false),
UpdateTime = table.Column<DateTime>(type: "TEXT", nullable: false),
CreateUserId = table.Column<long>(type: "INTEGER", nullable: false),
UpdateUserId = table.Column<long>(type: "INTEGER", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Products", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
Id = table.Column<string>(type: "TEXT", nullable: false),
UserName = table.Column<string>(type: "TEXT", nullable: true),
Password = table.Column<string>(type: "TEXT", nullable: true),
Email = table.Column<string>(type: "TEXT", nullable: true),
Phone = table.Column<string>(type: "TEXT", nullable: true),
QrCode = table.Column<string>(type: "TEXT", nullable: true),
CompanyCode = table.Column<string>(type: "TEXT", nullable: true),
DeptCode = table.Column<string>(type: "TEXT", nullable: true),
CreateTime = table.Column<DateTime>(type: "TEXT", nullable: false),
UpdateTime = table.Column<DateTime>(type: "TEXT", nullable: false),
CreateUserId = table.Column<long>(type: "INTEGER", nullable: false),
UpdateUserId = table.Column<long>(type: "INTEGER", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Posts",
columns: table => new
{
Id = table.Column<string>(type: "TEXT", nullable: false),
PostId = table.Column<int>(type: "INTEGER", nullable: false),
Title = table.Column<string>(type: "TEXT", nullable: true),
Content = table.Column<string>(type: "TEXT", nullable: true),
BlogId = table.Column<int>(type: "INTEGER", nullable: false),
BlogId1 = table.Column<string>(type: "TEXT", nullable: true),
CreateTime = table.Column<DateTime>(type: "TEXT", nullable: false),
UpdateTime = table.Column<DateTime>(type: "TEXT", nullable: false),
CreateUserId = table.Column<long>(type: "INTEGER", nullable: false),
UpdateUserId = table.Column<long>(type: "INTEGER", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Posts", x => x.Id);
table.ForeignKey(
name: "FK_Posts_Blogs_BlogId1",
column: x => x.BlogId1,
principalTable: "Blogs",
principalColumn: "Id");
});
migrationBuilder.CreateIndex(
name: "IX_Posts_BlogId1",
table: "Posts",
column: "BlogId1");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Posts");
migrationBuilder.DropTable(
name: "Products");
migrationBuilder.DropTable(
name: "Users");
migrationBuilder.DropTable(
name: "Blogs");
}
}
}

View File

@ -0,0 +1,170 @@
// <auto-generated />
using System;
using Bunny.Common.EFCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace Bunny.Common.Migrations
{
[DbContext(typeof(EfCoreContext))]
partial class EfCoreContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.7");
modelBuilder.Entity("Bunny.Dao.Models.System.Blog", b =>
{
b.Property<string>("Id")
.HasColumnType("TEXT");
b.Property<DateTime>("CreateTime")
.HasColumnType("TEXT");
b.Property<long>("CreateUserId")
.HasColumnType("INTEGER");
b.Property<DateTime>("UpdateTime")
.HasColumnType("TEXT");
b.Property<long>("UpdateUserId")
.HasColumnType("INTEGER");
b.Property<string>("Url")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Blogs");
});
modelBuilder.Entity("Bunny.Dao.Models.System.Post", b =>
{
b.Property<string>("Id")
.HasColumnType("TEXT");
b.Property<int>("BlogId")
.HasColumnType("INTEGER");
b.Property<string>("BlogId1")
.HasColumnType("TEXT");
b.Property<string>("Content")
.HasColumnType("TEXT");
b.Property<DateTime>("CreateTime")
.HasColumnType("TEXT");
b.Property<long>("CreateUserId")
.HasColumnType("INTEGER");
b.Property<int>("PostId")
.HasColumnType("INTEGER");
b.Property<string>("Title")
.HasColumnType("TEXT");
b.Property<DateTime>("UpdateTime")
.HasColumnType("TEXT");
b.Property<long>("UpdateUserId")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.HasIndex("BlogId1");
b.ToTable("Posts");
});
modelBuilder.Entity("Bunny.Dao.Models.System.Product", b =>
{
b.Property<string>("Id")
.HasColumnType("TEXT");
b.Property<DateTime>("CreateTime")
.HasColumnType("TEXT");
b.Property<long>("CreateUserId")
.HasColumnType("INTEGER");
b.Property<string>("CustomerId")
.HasColumnType("TEXT");
b.Property<string>("CustomerName")
.HasColumnType("TEXT");
b.Property<string>("PackageType")
.HasColumnType("TEXT");
b.Property<DateTime>("UpdateTime")
.HasColumnType("TEXT");
b.Property<long>("UpdateUserId")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.ToTable("Products");
});
modelBuilder.Entity("Bunny.Dao.Models.System.User", b =>
{
b.Property<string>("Id")
.HasColumnType("TEXT");
b.Property<string>("CompanyCode")
.HasColumnType("TEXT");
b.Property<DateTime>("CreateTime")
.HasColumnType("TEXT");
b.Property<long>("CreateUserId")
.HasColumnType("INTEGER");
b.Property<string>("DeptCode")
.HasColumnType("TEXT");
b.Property<string>("Email")
.HasColumnType("TEXT");
b.Property<string>("Password")
.HasColumnType("TEXT");
b.Property<string>("Phone")
.HasColumnType("TEXT");
b.Property<string>("QrCode")
.HasColumnType("TEXT");
b.Property<DateTime>("UpdateTime")
.HasColumnType("TEXT");
b.Property<long>("UpdateUserId")
.HasColumnType("INTEGER");
b.Property<string>("UserName")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Users");
});
modelBuilder.Entity("Bunny.Dao.Models.System.Post", b =>
{
b.HasOne("Bunny.Dao.Models.System.Blog", "Blog")
.WithMany()
.HasForeignKey("BlogId1");
b.Navigation("Blog");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -12,8 +12,4 @@ public class BaseModel
public long CreateUserId { get; set; }
public long UpdateUserId { get; set; }
public string? OperationMessage { get; set; }
public bool IsDelete { get; set; }
}

View File

@ -4,6 +4,5 @@ namespace Bunny.Dao.Models.System;
public class Blog : BaseModel
{
public int BlogId { get; set; }
public string Url { get; set; }
}

View File

@ -9,4 +9,22 @@ public interface IBlogService
/// </summary>
/// <param name="dto"></param>
void AddBlog(Blog dto);
/// <summary>
/// 查询BLog
/// </summary>
/// <returns></returns>
List<Blog> QueryBlog();
/// <summary>
/// 更新Blog内容
/// </summary>
/// <param name="dto"></param>
void UpdateBlog(Blog dto);
/// <summary>
/// 删除BLog
/// </summary>
/// <param name="id"></param>
void DeleteBlog(string id);
}

View File

@ -22,4 +22,34 @@ public class BlogService : IBlogService
_dbContext.Add(dto);
_dbContext.SaveChanges();
}
/// <summary>
/// 查询BLog
/// </summary>
/// <returns></returns>
public List<Blog> QueryBlog()
{
return _dbContext.Blogs.ToList();
}
/// <summary>
/// 更新Blog内容
/// </summary>
/// <param name="dto"></param>
public void UpdateBlog(Blog dto)
{
_dbContext.Blogs.Update(dto);
_dbContext.SaveChanges();
}
/// <summary>
/// 删除BLog
/// </summary>
/// <param name="id"></param>
public void DeleteBlog(string id)
{
var blog = new Blog { Id = id };
_dbContext.Blogs.Remove(blog);
_dbContext.SaveChanges();
}
}

View File

@ -20,7 +20,7 @@ public class EfCoreTest
// Read
Console.WriteLine("Querying for a blog");
var blog = db.Blogs
.OrderBy(b => b.BlogId)
.OrderBy(b => b.Id)
.First();
Console.WriteLine(blog.Url);
}

View File

@ -5,6 +5,9 @@ using Microsoft.AspNetCore.Mvc;
namespace Bunny.WebApi.Controllers;
/// <summary>
/// BLog相关接口
/// </summary>
[Route("/api/[controller]")]
public class BlogController : ControllerBase
{
@ -26,4 +29,38 @@ public class BlogController : ControllerBase
_blogService.AddBlog(dto);
return Result<string>.Success();
}
/// <summary>
/// 查询BLog
/// </summary>
/// <returns></returns>
[HttpGet("QueryBlog")]
public Result<List<Blog>> QueryBlog()
{
var vo = _blogService.QueryBlog();
return Result<List<Blog>>.Success(vo);
}
/// <summary>
/// 更新Blog内容
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
[HttpGet("UpdateBlog")]
public Result<string> UpdateBlog(Blog dto)
{
_blogService.UpdateBlog(dto);
return Result<string>.Success();
}
/// <summary>
/// 删除BLog
/// </summary>
/// <returns></returns>
[HttpDelete("DeleteBlog")]
public Result<string> DeleteBlog(string id)
{
_blogService.DeleteBlog(id);
return Result<string>.Success();
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.