public class BaseDto : INotifyPropertyChanged
{
public int Id { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// 实现通知更新
/// </summary>
/// <param name="propertyName"></param>
public void OnPropertyChanged([CallerMemberName] string propertyName="")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyToDo.Shared.Dtos
{
public class ResgiterUserDto : BaseDto
{
private string userName;
public string UserName
{
get { return userName; }
set { userName = value; OnPropertyChanged(); }
}
private string account;
public string Account
{
get { return account; }
set { account = value; OnPropertyChanged(); }
}
private string passWord;
public string PassWord
{
get { return passWord; }
set { passWord = value; OnPropertyChanged(); }
}
private string newpassWord;
public string NewPassWord
{
get { return newpassWord; }
set { newpassWord = value; OnPropertyChanged(); }
}
}
}
webApi programs
global using DotNet6WPFWebApi.Entities;
global using Microsoft.EntityFrameworkCore;
global using DotNet6WPFWebApi;
global using Microsoft.AspNetCore.Mvc;
using DotNet6WPFWebApi.Entities.Repository;
using DotNet6WPFWebApi.Service;
using AutoMapper;
using DotNet6WPFWebApi.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews().AddNewtonsoftJson();
//builder.Services.Configure<ApiBehaviorOptions>(options => options.SuppressModelStateInvalidFilter = true);
//屏蔽解码问题
builder.Services.Configure<ApiBehaviorOptions>((o) =>
{
o.SuppressModelStateInvalidFilter = true;
});
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddScoped<DbContext, MyDbContext>();
builder.Services.AddDbContext<MyDbContext>(opt =>
{
string connStr = builder.Configuration.GetSection("ConnectionStrings:SqliteConnectionString").Value;
opt.UseSqlite(connStr);
}).AddUnitOfWork<MyDbContext>()
.AddCustomRepository<ToDo, MyToDoRepository>()
.AddCustomRepository<User, MyUserRepository>()
.AddCustomRepository<ToDo, MyToDoRepository>();
builder.Services.AddTransient<IToDoService, ToDoService>();
builder.Services.AddTransient<IMemoService, MemoService>();
builder.Services.AddTransient<ILoginService, LoginService>();
var automapperConfog = new MapperConfiguration(config =>
{
config.AddProfile(new AutoMapperProFile());
});
builder.Services.AddSingleton(automapperConfog.CreateMapper());
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAuthorization();
app.MapControllers();
app.Run();
{
"ConnectionStrings": {
"SqliteConnectionString": "Data Source=D:\\Db\\DbSqlite1.db",
"MySQLConnectionString": "server=127.0.0.1; database=OneToMany; uid=root; pwd=123456;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
webApi配置
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="11.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.AutoHistory" Version="5.0.8" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="6.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
<PackageReference Include="NModbus4.NetCore" Version="2.0.1" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DotNet6WPFDemon.Shared\DotNet6WPFDemon.Shared.csproj" />
</ItemGroup>
</Project>
仓储:
using Microsoft.EntityFrameworkCore;
namespace DotNet6WPFWebApi.Entities
{
public class MyDbContext : DbContext
{
public DbSet<ToDo> ToDo { get; set; }
public DbSet<User> User { get; set; }
public DbSet<Memo> Memo { get; set; }
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
{
}
//protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
//{
// base.OnConfiguring(optionsBuilder);
//}
//protected override void OnModelCreating(ModelBuilder modelBuilder)
//{
// base.OnModelCreating(modelBuilder);
// modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
//}
}
}