C#
JsonExceptionが出た場合は下記をする。
出典: Asp.NetCoreApiのModel外部参照でNullを防ぐならIncludeを使え! — JsonExceptionが出た場合は下記をする。
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContext<DemoContext>(opt => opt.UseInMemoryDatabase("includeTest"));
services.AddControllers().AddNewtonsoftJson(opt =>
opt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore);
var options = new DbContextOptionsBuilder<DemoContext>().UseInMemoryDatabase(databaseName: "includeTest").Options;
using (var context = new DemoContext(options))
{
AddTestData(context);
}
}
▸ この snippet は実行結果未収録
▸ 実行結果は未収録です
Source収録記事
この snippet は記事の「JsonExceptionが出た場合は下記をする。」セクションに登場する。コードの前後の文脈・ハマりどころの解説は記事本文で。
同じ記事から
4 件namespace IncludeStudy.Models { public class Company {未収録
Asp.NetCoreApiのModel外部参照でNullが出る
#081405cf0fee
namespace IncludeStudy.Models { public class Employee {未収録
Asp.NetCoreApiのModel外部参照でNullが出る
#77f64dbdbb38
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks;未収録
Asp.NetCoreApiのModel外部参照でNullが出る
#87439b15ae9e
// GET: api/Companies/5 [HttpGet("{id}/emp")] public async Task<ActionResult<ICollection<Employee>>> GetCompanyEmp(int id) {未収録
Nullが出ないようにするにはIncludeを使う
#d7106e5aa49e
