1.增加一個實體類
/// <summary>
/// 團隊成員模型
/// </summary>
public class Member
{
/// <summary>
/// 團隊成員主鍵
/// </summary>
public int Id { set; get; }
/// <summary>
/// 團隊成員名
/// </summary>
public string FirstName { set; get; }
/// <summaryhua
/// 團隊成員花名
/// </summary>
public string NickName { set; get; }
/// <summary>
/// 團隊主鍵
/// </summary>
public int TeamId { set; get; }
}
2.增加一個數據庫上下文實體
public class MemberContext : DbContext
{
public MemberContext(DbContextOptions<MemberContext> options) : base(options)
{
}
public DbSet<Member> Members { get; set; }
}
3.注冊上下文到IOC容器
public void ConfigureServices(IServiceCollection services)
{
// 1、注冊上下文到IOC容器
services.AddDbContext<MemberContext>(options => {
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
});
}
4.appsettings.json 添加數據庫配置
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Data Source=.;Initial Catalog=memberservice;Persist Security Info=True;User ID=sa;Password=tony"
}
}
5.安裝EF Core 命令行工具
dotnet tool install --global dotnet-ef

6.安裝NuGet包 Microsoft.EntityFrameworkCore.Design
7.增加初始化遷移,更新數據庫
執行命令
dotnet ef migrations add test

執行成功后,在項目中會生成腳本操作

執行命令
dotnet ef database update

執行成功后,生成數據庫

8.添加服務接口
public interface IMemberService
{
IEnumerable<Member> GetMembers();
IEnumerable<Member> GetMembers(int teamId);
}
9.添加服務實現
public class MemberServiceImpl : IMemberService
{
public readonly IMemberRepository memberRepository;
public IEnumerable<Member> GetMembers()
{
return memberRepository.GetMembers();
}
public IEnumerable<Member> GetMembers(int teamId)
{
return memberRepository.GetMembers(teamId);
}
}
10.添加倉儲接口
public interface IMemberRepository
{
IEnumerable<Member> GetMembers();
IEnumerable<Member> GetMembers(int teamId);
}
11.添加倉促實現
public class MemberRepository : IMemberRepository
{
public MemberContext teamContext;
public MemberRepository(MemberContext teamContext)
{
this.teamContext = teamContext;
}
public IEnumerable<Member> GetMembers()
{
return teamContext.Members.ToList();
}
public IEnumerable<Member> GetMembers(int teamId)
{
return teamContext.Members.Where(memeber => memeber.TeamId == teamId);
}
}
12.注冊service和倉促
public void ConfigureServices(IServiceCollection services)
{
// 1、注冊上下文到IOC容器
services.AddDbContext<MemberContext>(opt =>
opt.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); //使用SqlServer數據庫
// 2、注冊成員service
services.AddScoped<IMemberService, MemberServiceImpl>();
// 3、注冊成員倉儲
services.AddScoped<IMemberRepository, MemberRepository>();
services.AddControllers();
}
13.添加Controller
[Route("api/[controller]")]
[ApiController]
public class MembersController : ControllerBase
{
private readonly IMemberService memberService;
public MembersController(IMemberService memberService)
{
this.memberService = memberService;
}
/// <summary>
/// 查詢所有成員信息
/// </summary>
/// <param name="teamId">?teamId參數結尾方式</param>
/// <returns></returns>
[HttpGet]
public ActionResult<IEnumerable<Member>> GetMembers(int teamId)
{
if (teamId == 0)
{
return memberService.GetMembers().ToList();
}
else
{
return memberService.GetMembers(teamId).ToList();
}
}
}
14.查看運行結果


