✨ feat(新增): EfCore使用添加方法添加内容示例
This commit is contained in:
parent
f0b1ecde21
commit
d8408a6792
|
@ -10,6 +10,15 @@
|
|||
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.6" />
|
||||
<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="System.IdentityModel.Tokens.Jwt" Version="7.6.0" />
|
||||
</ItemGroup>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
using Bunny.Dao.Entity.Blog;
|
||||
using Bunny.Dao.Models.System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Bunny.Common.EFCore;
|
||||
|
@ -7,15 +7,15 @@ public class EfCoreContext : DbContext
|
|||
{
|
||||
public EfCoreContext()
|
||||
{
|
||||
// var dbPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "bunny.db");
|
||||
// DbPath = Path.Join(dbPath, "bunny.db");
|
||||
// DbPath = @"D:\Project\web\Bunny-Cli\template\CSharp\CSharp-Single-EFCore\Bunny.WebApi\bunny.db";
|
||||
var dataBaseConnection = AppSettings.GetAppConfig<string>("DataBase:DataBaseConnection");
|
||||
DbPath = dataBaseConnection;
|
||||
DbPath = @"D:\Project\web\Bunny-Cli\template\CSharp\CSharp-Single-EFCore\Bunny.WebApi\Database\bunny.db";
|
||||
// var dataBaseConnection = AppSettings.GetAppConfig<string>("DataBase:DataBaseConnection");
|
||||
// DbPath = dataBaseConnection;
|
||||
}
|
||||
|
||||
public DbSet<Blog> Blogs { get; set; }
|
||||
public DbSet<Post> Posts { get; set; }
|
||||
public DbSet<Product> Products { get; set; }
|
||||
public DbSet<User> Users { get; set; }
|
||||
|
||||
public string DbPath { get; }
|
||||
|
||||
|
|
|
@ -8,15 +8,6 @@
|
|||
|
||||
<ItemGroup>
|
||||
<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>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -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; } = [];
|
||||
}
|
|
@ -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; }
|
||||
}
|
|
@ -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; }
|
||||
}
|
|
@ -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; }
|
||||
}
|
|
@ -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 string? Title { get; set; }
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Bunny.Dao.Models.System;
|
||||
|
||||
public class Product : BaseStuffModel
|
||||
public class Product : BaseModel
|
||||
{
|
||||
public string? CustomerId { get; set; }
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
using Bunny.Common.EFCore;
|
||||
using Bunny.Dao.Entity.Blog;
|
||||
using Bunny.Dao.Models.System;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Bunny.Test.Until.EfCoreTest;
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
using Bunny.Service.IService.Service;
|
||||
using Bunny.WebApi.Controllers.Base;
|
||||
using Bunny.WebApi.Controllers;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NUnit.Framework;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
|
||||
namespace Bunny.Test.Until.Redis;
|
||||
|
||||
[TestFixture]
|
||||
|
|
|
@ -18,7 +18,8 @@ public class BaseConfig
|
|||
{
|
||||
Console.Title = "Bunny Template";
|
||||
|
||||
// 注入服务依赖
|
||||
// 注入控制器、服务依赖、Host服务等
|
||||
builder.AddApplicationController();
|
||||
builder.AddApplicationServices();
|
||||
builder.AddApplicationBackendServices();
|
||||
|
||||
|
|
|
@ -1,20 +1,28 @@
|
|||
using Bunny.Service.BackgroundModule;
|
||||
using Bunny.Common.EFCore;
|
||||
using Bunny.Service.IService;
|
||||
using Bunny.Service.IService.Service;
|
||||
using Bunny.WebApi.Controllers.Base;
|
||||
using Bunny.WebApi.Controllers;
|
||||
|
||||
namespace Bunny.WebApi.Config;
|
||||
|
||||
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)
|
||||
{
|
||||
builder.Services.AddScoped<TemplateController>();
|
||||
builder.Services.AddScoped<EfCoreContext>();
|
||||
builder.Services.AddScoped<IBaseService, BaseService>();
|
||||
builder.Services.AddScoped<IBlogService, BlogService>();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 注入后台服务相关
|
||||
/// 注入后台服务相关
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
public static void AddApplicationBackendServices(this WebApplicationBuilder builder)
|
||||
|
@ -22,4 +30,4 @@ public static class ServiceRegistration
|
|||
// 测试注入后台服务
|
||||
// builder.Services.AddHostedService<TemplateBackgroundModule>();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Bunny.WebApi.Controllers.Base;
|
||||
namespace Bunny.WebApi.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// 主页请求
|
||||
|
@ -16,6 +16,6 @@ public class IndexController : ControllerBase
|
|||
[HttpGet("")]
|
||||
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");
|
||||
}
|
||||
}
|
|
@ -1,12 +1,8 @@
|
|||
using Bunny.Common.EFCore;
|
||||
using Bunny.Common.Exception;
|
||||
using Bunny.Dao.Entity.Blog;
|
||||
using Bunny.Dao.Models.System;
|
||||
using Bunny.Dao.Result;
|
||||
using Bunny.Dao.Result;
|
||||
using Bunny.Service.IService;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Bunny.WebApi.Controllers.Base;
|
||||
namespace Bunny.WebApi.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// 测试请求模板
|
||||
|
@ -34,7 +30,7 @@ public class TemplateController : ControllerBase
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试Service是否注入
|
||||
/// 测试Service是否注入
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet("TestInject")]
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue