这是名为学生的模型
using System;
namespace peakR.Model.DBModels
{
public partial class Student: BaseEntity
{
//public long Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public DateTime? DateOfBirth { get; set; }
public DateTime? DateOfRegistration { get; set; }
public string PhoneNumber { get; set; }
public string Email { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
}
这是学生实体框架的映射,因为我首先使用代码approch
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using peakR.Model.DBModels;
namespace peakR.Model.Mapping
{
public class StudentMap
{
public StudentMap(EntityTypeBuilder<Student> entityBuilder)
{
entityBuilder.HasKey(t => t.Id);
entityBuilder.Property(e => e.FirstName)
.HasMaxLength(30)
.IsUnicode(false);
entityBuilder.Property(e => e.Address1)
.HasMaxLength(50)
.IsUnicode(false);
entityBuilder.Property(e => e.Address2)
.HasMaxLength(50)
.IsUnicode(false);
entityBuilder.Property(e => e.City)
.HasMaxLength(30)
.IsUnicode(false);
entityBuilder.Property(e =>
e.DateOfBirth).HasColumnType("datetime");
entityBuilder.Property(e =>
e.DateOfRegistration).HasColumnType("datetime");
entityBuilder.Property(e => e.Email)
.HasMaxLength(50)
.IsUnicode(false);
entityBuilder.Property(e => e.FirstName)
.HasMaxLength(30)
.IsUnicode(false);
entityBuilder.Property(e => e.Gender)
.HasMaxLength(10)
.IsUnicode(false);
entityBuilder.Property(e => e.LastName)
.HasMaxLength(30)
.IsUnicode(false);
entityBuilder.Property(e => e.PhoneNumber)
.HasMaxLength(20)
.IsUnicode(false);
entityBuilder.Property(e => e.State)
.HasMaxLength(30)
.IsUnicode(false);
entityBuilder.Property(e => e.Zip).HasColumnType("nchar(10)");
}
}
}
这是id&#39>
的基础实体public class BaseEntity
{
/// <summary>
/// Gets or sets the entity identifier
/// </summary>
public Guid Id { get; set; }
这是方法的存储库
using Microsoft.EntityFrameworkCore;
using peakR.Model;
using System;
using System.Collections.Generic;
using System.Linq;
namespace peakR.Data
{
/// <summary>
/// Entity Framework Core repository
/// </summary>
public class EfRepository<T> : IRepository<T> where T : BaseEntity
{
#region Fields
private readonly peakRContext context;
private DbSet<T> entities;
string errorMessage = string.Empty;
#endregion
#region Ctor
public EfRepository(peakRContext context)
{
this.context = context;
entities = context.Set<T>();
}
#endregion
#region Methods
/// <summary>
/// Gets a table
/// </summary>
public IEnumerable<T> GetAll()
{
return entities.AsEnumerable();
}
/// <summary>
/// Get entity by identifier
/// </summary>
/// <param name="id">Identifier</param>
/// <returns>Entity</returns>
public T Get(Guid id)
{
return entities.SingleOrDefault(s => s.Id == id);
}
/// <summary>
/// Insert entity
/// </summary>
/// <param name="entity">Entity</param>
public void Insert(T entity)
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
entities.Add(entity);
context.SaveChanges();
}
/// <summary>
/// Update entity
/// </summary>
/// <param name="entity">Entity</param>
public void Update(T entity)
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
context.SaveChanges();
}
/// <summary>
/// Delete entity
/// </summary>
/// <param name="entity">Entity</param>
public void Delete(T entity)
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
entities.Remove(entity);
context.SaveChanges();
}
#endregion
}
}
这是我们模特的服务
using peakR.Data;
using peakR.Model.DBModels;
using System;
using System.Collections.Generic;
namespace peakR.Services.Students
{
/// <summary>
/// Student service
/// </summary>
public class StudentService : IStudentService
{
#region Fields
private readonly IRepository<Student> _studentRepository;
#endregion
#region Ctor
public StudentService(IRepository<Student> studentRepository)
{
this._studentRepository = studentRepository;
}
#endregion
#region Methods
/// <summary>
/// Gets all the students
/// </summary>
/// <returns>List of Student</returns>
public virtual IEnumerable<Student> GetAllStudents()
{
return _studentRepository.GetAll();
}
/// <summary>
/// Gets a Student
/// </summary>
/// <param name="studentId">Student identifier</param>
/// <returns>Student</returns>
public virtual Student GetStudentById(Guid studentId)
{
if (studentId.Equals(Guid.Empty))
return null;
return _studentRepository.Get(studentId);
}
/// <summary>
/// Inserts student
/// </summary>
/// <param name="student">student</param>
public virtual void InsertStudent(Student student)
{
if (student == null)
throw new ArgumentNullException(nameof(student));
student.Id = new Guid();
_studentRepository.Insert(student);
}
/// <summary>
/// Updates the student
/// </summary>
/// <param name="student">student</param>
public virtual void UpdateStudent(Student student)
{
if (student == null)
throw new ArgumentNullException(nameof(student));
_studentRepository.Update(student);
}
/// <summary>
/// Delete the student
/// </summary>
/// <param name="student">student</param>
public virtual void DeleteStudent(Student student)
{
if (student == null)
throw new ArgumentNullException(nameof(student));
_studentRepository.Delete(student);
}
#endregion
}
}
我创建了名为值Controller的控制器
using log4net;
using Microsoft.AspNetCore.Mvc;
using peakR.Services.Students;
using peakR.Web.Infrastructure.Mapper;
using peakR.Web.ViewModels;
using System;
namespace peakR.Web.Controllers
{
[Route("api/Values")]
public class ValuesController : Controller
{
public static log4net.ILog log { get; set; }
ILog logger = log4net.LogManager.GetLogger(typeof(ValuesController));
private IStudentService _studentService;
public ValuesController(IStudentService studentService)
{
this._studentService = studentService;
}
// GET api/values
[HttpGet]
public IActionResult Get()
{
logger.Info(", Value, GetAll Is Invoked");
//logger.Debug("DebugGetall is involked");
//logger.Error("error");
return Json(_studentService.GetAllStudents());
}
// GET api/values/5
[HttpGet("{id}")]
public IActionResult Get(Guid id)
{
logger.Debug("value,GetById Is Invoked");
var student = _studentService.GetStudentById(id);
if (student == null)
{
return Json("");
}
return Json(_studentService.GetStudentById(id));
}
// POST api/values
[HttpPost]
public void Post([FromBody]StudentModel student)
{
logger.Info("value,Post Is Invoked");
if (student != null)
_studentService.InsertStudent(student.ToEntity());
}
// PUT api/values/5
[HttpPut("{id}")]
public void Put(Guid id, [FromBody]StudentModel student)
{
logger.Info(",value,Put Is Invoked");
if (id == student.Id)
{
var dbStudent = _studentService.GetStudentById(student.Id);
if (dbStudent!=null)
{
dbStudent = student.ToEntity(dbStudent);
_studentService.UpdateStudent(dbStudent);
}
}
}
// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(Guid id)
{
logger.Info("value,Delete Is Invoked");
var dbStudent = _studentService.GetStudentById(id);
if (dbStudent != null)
{
_studentService.DeleteStudent(dbStudent);
}
}
}
}
当我从邮递员那里取数据或直接打电话给api&#34; [&#34;来了。 没有显示任何内容 数据未来请帮助我。 如果你想要什么,请告诉我