。net核心中邮递员的数据并不令人失望

问题描述 投票:1回答:1

这是模型,名称为学生

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; }
}
}

这是实体框架的学生映射,因为我正在使用代码优先方法

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的基本实体

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
}
}

我已创建名为值C​​ontroller的控制器

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时,只会出现[[]。什么都没显示数据即将到来,请帮助我。如有需要,请告诉我

asp.net-core ef-code-first ef-core-2.0
1个回答
0
投票

感谢我的连接字符串有问题。谢谢

© www.soinside.com 2019 - 2024. All rights reserved.