動くコード図鑑技術記事現場の渡り方キャリア論すべての記事About
C#

Asp.NetCoreApiのModel外部参照でNullが出る

出典: Asp.NetCoreApiのModel外部参照でNullを防ぐならIncludeを使え!Asp.NetCoreApiのModel外部参照でNullが出る

Asp.NetCoreApiのModel外部参照でNullが出る (csharp)#87439b15ae9e
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 は実行結果未収録
▸ 実行結果は未収録です
  • id: #87439b15ae9e
  • lines: 123
  • extracted: 2026-06-10

Source収録記事

この snippet は記事の「Asp.NetCoreApiのModel外部参照でNullが出る」セクションに登場する。コードの前後の文脈・ハマりどころの解説は記事本文で。

同じ記事から

4
図鑑トップ