feat(新增): EfCore使用添加方法添加内容示例

This commit is contained in:
Bunny 2024-08-09 00:16:40 +08:00
parent f0b1ecde21
commit d8408a6792
21 changed files with 119 additions and 72 deletions

View File

@ -10,6 +10,15 @@
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" /> <PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.6" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.6.0" /> <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.6.0" />
</ItemGroup> </ItemGroup>

View File

@ -1,4 +1,4 @@
using Bunny.Dao.Entity.Blog; using Bunny.Dao.Models.System;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace Bunny.Common.EFCore; namespace Bunny.Common.EFCore;
@ -7,15 +7,15 @@ public class EfCoreContext : DbContext
{ {
public EfCoreContext() public EfCoreContext()
{ {
// var dbPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "bunny.db"); DbPath = @"D:\Project\web\Bunny-Cli\template\CSharp\CSharp-Single-EFCore\Bunny.WebApi\Database\bunny.db";
// DbPath = Path.Join(dbPath, "bunny.db"); // var dataBaseConnection = AppSettings.GetAppConfig<string>("DataBase:DataBaseConnection");
// DbPath = @"D:\Project\web\Bunny-Cli\template\CSharp\CSharp-Single-EFCore\Bunny.WebApi\bunny.db"; // DbPath = dataBaseConnection;
var dataBaseConnection = AppSettings.GetAppConfig<string>("DataBase:DataBaseConnection");
DbPath = dataBaseConnection;
} }
public DbSet<Blog> Blogs { get; set; } public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; } public DbSet<Post> Posts { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<User> Users { get; set; }
public string DbPath { get; } public string DbPath { get; }

View File

@ -8,15 +8,6 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0"/> <PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,9 +0,0 @@
namespace Bunny.Dao.Entity.Blog;
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public List<Post> Posts { get; } = [];
}

View File

@ -1,11 +0,0 @@

namespace Bunny.Dao.Models.Base;
public class BasePersonModel : BaseModel
{
public string? Name { get; set; }
public string? Type { get; set; }
public string? Description { get; set; }
}

View File

@ -1,15 +0,0 @@

namespace Bunny.Dao.Models.Base;
public class BaseStuffModel : BaseModel
{
public string? Name { get; set; }
public string? Type { get; set; }
public string? Description { get; set; }
public string? Spec { get; set; }
public bool IsUse { get; set; }
}

View File

@ -0,0 +1,9 @@
using Bunny.Dao.Models.Base;
namespace Bunny.Dao.Models.System;
public class Blog : BaseModel
{
public int BlogId { get; set; }
public string Url { get; set; }
}

View File

@ -1,6 +1,8 @@
namespace Bunny.Dao.Entity.Blog; using Bunny.Dao.Models.Base;
public class Post namespace Bunny.Dao.Models.System;
public class Post : BaseModel
{ {
public int PostId { get; set; } public int PostId { get; set; }
public string? Title { get; set; } public string? Title { get; set; }

View File

@ -2,7 +2,7 @@
namespace Bunny.Dao.Models.System; namespace Bunny.Dao.Models.System;
public class Product : BaseStuffModel public class Product : BaseModel
{ {
public string? CustomerId { get; set; } public string? CustomerId { get; set; }

View File

@ -0,0 +1,12 @@
using Bunny.Dao.Models.System;
namespace Bunny.Service.IService;
public interface IBlogService
{
/// <summary>
/// 添加Blog
/// </summary>
/// <param name="dto"></param>
void AddBlog(Blog dto);
}

View File

@ -0,0 +1,25 @@
using Bunny.Common.EFCore;
using Bunny.Dao.Models.System;
namespace Bunny.Service.IService.Service;
public class BlogService : IBlogService
{
private readonly EfCoreContext _dbContext;
public BlogService(EfCoreContext dbContext)
{
_dbContext = dbContext;
}
/// <summary>
/// 添加Blog
/// </summary>
/// <param name="dto"></param>
/// <exception cref="NotImplementedException"></exception>
public void AddBlog(Blog dto)
{
_dbContext.Add(dto);
_dbContext.SaveChanges();
}
}

View File

@ -1,5 +1,5 @@
using Bunny.Common.EFCore; using Bunny.Common.EFCore;
using Bunny.Dao.Entity.Blog; using Bunny.Dao.Models.System;
using NUnit.Framework; using NUnit.Framework;
namespace Bunny.Test.Until.EfCoreTest; namespace Bunny.Test.Until.EfCoreTest;

View File

@ -1,5 +1,5 @@
using Bunny.Service.IService.Service; using Bunny.Service.IService.Service;
using Bunny.WebApi.Controllers.Base; using Bunny.WebApi.Controllers;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework; using NUnit.Framework;
using Xunit; using Xunit;

View File

@ -18,7 +18,8 @@ public class BaseConfig
{ {
Console.Title = "Bunny Template"; Console.Title = "Bunny Template";
// 注入服务依赖 // 注入控制器、服务依赖、Host服务等
builder.AddApplicationController();
builder.AddApplicationServices(); builder.AddApplicationServices();
builder.AddApplicationBackendServices(); builder.AddApplicationBackendServices();

View File

@ -1,20 +1,28 @@
using Bunny.Service.BackgroundModule; using Bunny.Common.EFCore;
using Bunny.Service.IService; using Bunny.Service.IService;
using Bunny.Service.IService.Service; using Bunny.Service.IService.Service;
using Bunny.WebApi.Controllers.Base; using Bunny.WebApi.Controllers;
namespace Bunny.WebApi.Config; namespace Bunny.WebApi.Config;
public static class ServiceRegistration public static class ServiceRegistration
{ {
public static void AddApplicationController(this WebApplicationBuilder builder)
{
builder.Services.AddScoped<IndexController>();
builder.Services.AddScoped<TemplateController>();
builder.Services.AddScoped<BlogController>();
}
public static void AddApplicationServices(this WebApplicationBuilder builder) public static void AddApplicationServices(this WebApplicationBuilder builder)
{ {
builder.Services.AddScoped<TemplateController>(); builder.Services.AddScoped<EfCoreContext>();
builder.Services.AddScoped<IBaseService, BaseService>(); builder.Services.AddScoped<IBaseService, BaseService>();
builder.Services.AddScoped<IBlogService, BlogService>();
} }
/// <summary> /// <summary>
/// 注入后台服务相关 /// 注入后台服务相关
/// </summary> /// </summary>
/// <param name="builder"></param> /// <param name="builder"></param>
public static void AddApplicationBackendServices(this WebApplicationBuilder builder) public static void AddApplicationBackendServices(this WebApplicationBuilder builder)

View File

@ -0,0 +1,29 @@
using Bunny.Dao.Models.System;
using Bunny.Dao.Result;
using Bunny.Service.IService;
using Microsoft.AspNetCore.Mvc;
namespace Bunny.WebApi.Controllers;
[Route("/api/[controller]")]
public class BlogController : ControllerBase
{
private readonly IBlogService _blogService;
public BlogController(IBlogService blogService)
{
_blogService = blogService;
}
/// <summary>
/// 添加Blog
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
[HttpGet("AddBlog")]
public Result<string> AddBlog(Blog dto)
{
_blogService.AddBlog(dto);
return Result<string>.Success();
}
}

View File

@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace Bunny.WebApi.Controllers.Base; namespace Bunny.WebApi.Controllers;
/// <summary> /// <summary>
/// 主页请求 /// 主页请求
@ -16,6 +16,6 @@ public class IndexController : ControllerBase
[HttpGet("")] [HttpGet("")]
public IActionResult Index() public IActionResult Index()
{ {
return Ok("看到这里页面说明你已经成功启动了本项目:)\n\n如果觉得项目有用打赏作者喝杯咖啡作为奖励>>https://gitee.com/BunnyBoss/bunny-template.git"); return Ok("看到这里页面说明你已经成功启动了本项目:)\n\n如果觉得项目有用打赏作者喝杯咖啡作为奖励>>https://gitee.com/BunnyBoss/csharp-single-efcore");
} }
} }

View File

@ -1,12 +1,8 @@
using Bunny.Common.EFCore; using Bunny.Dao.Result;
using Bunny.Common.Exception;
using Bunny.Dao.Entity.Blog;
using Bunny.Dao.Models.System;
using Bunny.Dao.Result;
using Bunny.Service.IService; using Bunny.Service.IService;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace Bunny.WebApi.Controllers.Base; namespace Bunny.WebApi.Controllers;
/// <summary> /// <summary>
/// 测试请求模板 /// 测试请求模板
@ -34,7 +30,7 @@ public class TemplateController : ControllerBase
} }
/// <summary> /// <summary>
/// 测试Service是否注入 /// 测试Service是否注入
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
[HttpGet("TestInject")] [HttpGet("TestInject")]

Binary file not shown.

Binary file not shown.