成功写入Uesrs数据库表

This commit is contained in:
2025-07-07 18:41:36 +08:00
parent a3996dad99
commit 547142730e
17 changed files with 170 additions and 59 deletions

View File

@ -0,0 +1,9 @@
namespace WorkStation.Server.Context.Entity
{
public class BaseEntity
{
public int Id { get; set; }
public DateTime CreateTime { get; set; }
public DateTime UpdateTime { get; set; }
}
}

View File

@ -0,0 +1,8 @@
namespace WorkStation.Server.Context.Entity
{
public class User : BaseEntity
{
public string? Account { get; set; }
public string? Password { get; set; }
}
}

View File

@ -0,0 +1,10 @@
using Microsoft.EntityFrameworkCore;
using WorkStation.Server.Context.Entity;
namespace WorkStation.Server.Context
{
public class MyDbContext(DbContextOptions<MyDbContext> options) : DbContext(options)
{
public DbSet<User> Users { get; set; }
}
}

View File

@ -0,0 +1,9 @@
using Arch.EntityFrameworkCore.UnitOfWork;
using WorkStation.Server.Context.Entity;
namespace WorkStation.Server.Context.Repository
{
public class UserRepository(MyDbContext context) : Repository<User>(context), IRepository<User>
{
}
}

View File

@ -0,0 +1,17 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WorkStation.Server.Context.Entity;
using WorkStation.Server.Services;
using WorkStation.Share;
namespace WorkStation.Server.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class UserController(IUserService service) : ControllerBase
{
private readonly IUserService _service = service;
[HttpPost]
public Task<ApiResponse> Add(User item) => _service.AddAsync(item);
}
}

View File

@ -1,33 +0,0 @@
using Microsoft.AspNetCore.Mvc;
namespace WorkStation.Server.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}

View File

@ -1,7 +1,22 @@
using Arch.EntityFrameworkCore.UnitOfWork;
using Microsoft.EntityFrameworkCore;
using WorkStation.Server.Context;
using WorkStation.Server.Context.Entity;
using WorkStation.Server.Context.Repository;
using WorkStation.Server.Services;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("MySQLConnection");
builder.Services.AddDbContext<MyDbContext>(options =>
{
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
});
builder.Services.AddUnitOfWork<MyDbContext>();
builder.Services.AddCustomRepository<User, UserRepository>();
builder.Services.AddTransient<IUserService, UserService>();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
@ -9,6 +24,7 @@ builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{

View File

@ -0,0 +1,14 @@
using WorkStation.Share;
namespace WorkStation.Server.Services
{
public interface IBaseSerivice<T>
{
Task<ApiResponse> AddAsync(T item);
Task<ApiResponse> DelateAsync(int id);
Task<ApiResponse> UpdateAsync(T item);
Task<ApiResponse> GetByIdAsync(int id);
Task<ApiResponse> GetAllAsync();
}
}

View File

@ -0,0 +1,9 @@
using WorkStation.Server.Context.Entity;
namespace WorkStation.Server.Services
{
public interface IUserService : IBaseSerivice<User>
{
}
}

View File

@ -0,0 +1,38 @@
using Arch.EntityFrameworkCore.UnitOfWork;
using WorkStation.Server.Context.Entity;
using WorkStation.Share;
namespace WorkStation.Server.Services
{
public class UserService(IUnitOfWork unitOfWork) : IUserService
{
private IUnitOfWork _unitOfWork = unitOfWork;
public async Task<ApiResponse> AddAsync(User item)
{
var repository = _unitOfWork.GetRepository<User>();
await repository.InsertAsync(item);
var count = await _unitOfWork.SaveChangesAsync();
return new ApiResponse(true, "添加数据", count);
}
public Task<ApiResponse> DelateAsync(int id)
{
throw new NotImplementedException();
}
public Task<ApiResponse> GetAllAsync()
{
throw new NotImplementedException();
}
public Task<ApiResponse> GetByIdAsync(int id)
{
throw new NotImplementedException();
}
public Task<ApiResponse> UpdateAsync(User item)
{
throw new NotImplementedException();
}
}
}

View File

@ -1,13 +0,0 @@
namespace WorkStation.Server
{
public class WeatherForecast
{
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}
}

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
@ -7,7 +7,18 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.13">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.UnitOfWork" Version="3.1.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WorkStation.Share\WorkStation.Share.csproj" />
</ItemGroup>
</Project>

View File

@ -5,5 +5,8 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"ConnectionStrings": {
"MySQLConnection": "server=106.14.189.125;Database=WorkStationDB;User=root;Password=Mysql!950307"
}
}