小弟最近在學blazor 找到一個範例
https://www.youtube.com/watch?v=DHe4UghuQkU&t=1005s
程式碼都一模一樣 但新增與刪除功能不會實踐
資料庫
CREATE TABLE [dbo].[Employee](
[EmployeeId] [int] IDENTITY(1,1) NOT NULL,
[Nane] [nvarchar](50) NULL,
[Division] [nvarchar](50) NULL,
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
(
[EmployeeId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
Employee.cs
using System;
using System.Collections.Generic;
namespace BlazorApp1.Models
{
public partial class Employee
{
public int EmployeeId { get; set; }
public string Nane { get; set; }
public string Division { get; set; }
}
}
TbkcContext.cs
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
namespace BlazorApp1.Models
{
public partial class TbkcContext : DbContext
{
public TbkcContext()
{
}
public TbkcContext(DbContextOptions<TbkcContext> options)
: base(options)
{
}
public virtual DbSet<Employee> Employee { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Employee>(entity =>
{
entity.Property(e => e.Division).HasMaxLength(50);
entity.Property(e => e.Nane).HasMaxLength(50);
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
}
EmployeeService.cs
using BlazorApp1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BlazorApp1.Data
{
public class EmployeeService
{
private readonly TbkcContext _context;
public EmployeeService(TbkcContext context)
{
_context = context;
}
public List<Employee> GetEmployees()
{
return _context.Employee.ToList();
}
public void AddEmployee(Employee insert)
{
_context.Employee.Add(insert);
_context.SaveChanges();
}
public void DelEmployee(Employee del)
{
_context.Employee.Remove(del);
_context.SaveChanges();
}
}
}
EmployeePage.razor
@page "/employeePage"
@using BlazorApp1.Data
@using BlazorApp1.Models
@inject EmployeeService EmployeeService
<h1>Employee</h1>
<div>姓名:<input id="Text1" type="text" @bind="addEmployee.Nane" /></div>
<div>科室:<input id="Text1" type="text" @bind="addEmployee.Division" /></div>
<div><button @onclick="AddEmployee">新增</button></div>
<hr>
@if (employeesdata == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>編號</th>
<th>姓名</th>
<th>科室</th>
<th>編輯</th>
</tr>
</thead>
<tbody>
@foreach (var item in employeesdata)
{
<tr>
<td>@item.EmployeeId</td>
<td>@item.Nane</td>
<td>@item.Division</td>
<td><button @onclick="()=>DelEmployee(item)">刪除</button></td>
</tr>
}
</tbody>
</table>
}
@code {
private List<Employee> employeesdata;
public Employee addEmployee = new Employee()
{
Nane = "",
Division = ""
};
protected override void OnInitialized()
{
Getemployee();
}
public void Getemployee()
{
employeesdata = EmployeeService.GetEmployees();
}
public void AddEmployee()
{
EmployeeService.AddEmployee(addEmployee);
addEmployee = new Employee()
{
Nane = "",
Division = ""
};
Getemployee();
}
public void DelEmployee(Employee del)
{
EmployeeService.DelEmployee(del);
Getemployee();
}
}
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"TbkcDatabase": "Data Source=127.0.0.1;Initial Catalog=Tbkc;Persist Security Info=True;User ID=sa;Password=123456"
}
}
資料可以讀到 但新增跟刪除無法運作