初始化数据库模型

This commit is contained in:
2025-10-18 14:53:04 +08:00
7 changed files with 58 additions and 5 deletions
@@ -0,0 +1,10 @@
namespace IM_API.Configs
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddAllService(this IServiceCollection services, IConfiguration configuration)
{
return services;
}
}
}
+8
View File
@@ -0,0 +1,8 @@
namespace IM_API.Dtos
{
public class TestDto
{
public int Id { get; set; }
public string Name { get; set; }
}
}
@@ -0,0 +1,9 @@
using IM_API.Dtos;
namespace IM_API.Interface.Services
{
public interface IDemo
{
TestDto Test();
}
}
-4
View File
@@ -50,10 +50,6 @@ public partial class IMDbContext : DbContext
public virtual DbSet<User> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see https://go.microsoft.com/fwlink/?LinkId=723263.
=> optionsBuilder.UseMySql("server=192.168.5.100;port=3306;database=IM;user=root;password=768788Dyw", Microsoft.EntityFrameworkCore.ServerVersion.Parse("5.7.44-mysql"));
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
+14
View File
@@ -1,4 +1,8 @@
using IM_API.Configs;
using IM_API.Models;
using Microsoft.EntityFrameworkCore;
namespace IM_API
{
public class Program
@@ -8,7 +12,17 @@ namespace IM_API
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
string conStr = builder.Configuration.GetConnectionString("DefaultConnection")!;
builder.Services.AddDbContext<IMDbContext>(options =>
{
options.UseMySql(conStr,ServerVersion.AutoDetect(conStr));
});
builder.Services.AddAllService(configuration);
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
+13
View File
@@ -0,0 +1,13 @@
using IM_API.Dtos;
using IM_API.Interface.Services;
namespace IM_API.Services
{
public class Demo : IDemo
{
public TestDto Test()
{
throw new NotImplementedException();
}
}
}
+4 -1
View File
@@ -5,5 +5,8 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Server=192.168.5.100;Port=3306;Database=IM;User=root;Password=768788Dyw;"
}
}