Two models:
public class Item
{
public int Id { get; set; }
public string Title { get; set; }
public int? StorageId { get; set; }
public virtual Storage? Storage { get; set; }
}
public class Storage
{
public int Id { get; set; }
public string StorageName { get; set; }
public virtual ICollection<Item>? Items { get; set; }
}
I want to create a table where I could see a storage name for every item. How to achieve that using web api? Is there any LINQ method or I should create some more properties in Item model?
I am getting a JSON possible cycle error if I use this:
public async Task<ActionResult<IEnumerable<Item>>> GetItems()
{
var item = await _context.Items.Include(c => c.Storage).ToListAsync();
return item;
}
I saw the way to avoid cycle error using
builder.Services
.AddControllersWithViews()
.AddJsonOptions(options => options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles);
But not sure it is a good practice..
Thanks in advance