C#
Asp.NetCoreApiのModel外部参照でNullが出る
出典: Asp.NetCoreApiのModel外部参照でNullを防ぐならIncludeを使え! — Asp.NetCoreApiのModel外部参照でNullが出る
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using IncludeStudy.Models;
namespace IncludeStudy.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class CompaniesController : ControllerBase
{
private readonly DemoContext _context;
public CompaniesController(DemoContext context)
{
_context = context;
}
// GET: api/Companies
[HttpGet]
public async Task<ActionResult<IEnumerable<Company>>> GetCompany()
{
return await _context.Company.ToListAsync();
}
// GET: api/Companies/5
[HttpGet("{id}")]
public async Task<ActionResult<Company>> GetCompany(int id)
{
var company = await _context.Company.FindAsync(id);
if (company == null)
{
return NotFound();
}
return company;
}
// GET: api/Companies/5
[HttpGet("{id}/emp")]
public async Task<ActionResult<ICollection<Employee>>> GetCompanyEmp(int id)
{
var company = await _context.Company.FindAsync(id);
if (company == null)
{
return NotFound();
}
return company.Employees.ToList();
}
// PUT: api/Companies/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see https://go.microsoft.com/fwlink/?linkid=2123754.
[HttpPut("{id}")]
public async Task<IActionResult> PutCompany(int id, Company company)
{
if (id != company.Id)
{
return BadRequest();
}
_context.Entry(company).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!CompanyExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Companies
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see https://go.microsoft.com/fwlink/?linkid=2123754.
[HttpPost]
public async Task<ActionResult<Company>> PostCompany(Company company)
{
_context.Company.Add(company);
await _context.SaveChangesAsync();
return CreatedAtAction("GetCompany", new { id = company.Id }, company);
}
// DELETE: api/Companies/5
[HttpDelete("{id}")]
public async Task<ActionResult<Company>> DeleteCompany(int id)
{
var company = await _context.Company.FindAsync(id);
if (company == null)
{
return NotFound();
}
_context.Company.Remove(company);
await _context.SaveChangesAsync();
return company;
}
private bool CompanyExists(int id)
{
return _context.Company.Any(e => e.Id == id);
}
}
}
▸ この snippet は実行結果未収録
▸ 実行結果は未収録です
Source収録記事
この snippet は記事の「Asp.NetCoreApiのModel外部参照でNullが出る」セクションに登場する。コードの前後の文脈・ハマりどころの解説は記事本文で。
同じ記事から
4 件namespace IncludeStudy.Models { public class Company {未収録
Asp.NetCoreApiのModel外部参照でNullが出る
#081405cf0fee
namespace IncludeStudy.Models { public class Employee {未収録
Asp.NetCoreApiのModel外部参照でNullが出る
#77f64dbdbb38
// GET: api/Companies/5 [HttpGet("{id}/emp")] public async Task<ActionResult<ICollection<Employee>>> GetCompanyEmp(int id) {未収録
Nullが出ないようにするにはIncludeを使う
#d7106e5aa49e
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers();未収録
JsonExceptionが出た場合は下記をする。
#7cf68e77babf
