I'm trying to add Breeze to my .Net Core 2.2 webapi and I can't figure out what I'm missing. To troubleshoot I've created a very simple webapi that returns 1 item. This works but breeze isn't adding it's custom properties to my entities.
I've added [BreezeQueryFilter] to my controller but the $id and $type properties are not being added to my entities.
I've create a simple repository with what I have so far.
https://github.com/wstow/SimpleBreeze
Thanks
My Controller
[Route("api/[controller]/[action]")]
[BreezeQueryFilter]
public class OrderController : Controller
{
private OrderContext _context;
private OrderManager PersistenceManager;
public OrderController(OrderContext context)
{
this._context = context;
PersistenceManager = new OrderManager(context);
}
[HttpGet]
public IActionResult Metadata()
{
return Ok(PersistenceManager.Metadata());
}
[HttpGet]
public IQueryable<ReqStatus> Status()
{
return PersistenceManager.Context.ReqStatus;
}
}
My Manager
public class OrderManager : EFPersistenceManager<OrderContext>
{
public OrderManager(OrderContext orderContext) : base(orderContext) { }
}
My Context
public class OrderContext : DbContext
{
public OrderContext()
{
//Configuration.ProxyCreationEnabled = false;
// Configuration.LazyLoadingEnabled = false;
}
public OrderContext(DbContextOptions<OrderContext> options)
: base(options)
{ }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{ }
public DbSet<ReqStatus> ReqStatus { get; set; }
}
The problem is in the JSON serialization settings. Newtonsoft.Json is highly configurable, and you need to use the right settings to communicate properly with the Breeze client.
To make this easy, Breeze has a configuration function to change the settings to good defaults. You call it from your Startup.cs
:
public void ConfigureServices(IServiceCollection services)
{
var mvcBuilder = services.AddMvc();
mvcBuilder.AddJsonOptions(opt => {
var ss = JsonSerializationFns.UpdateWithDefaults(opt.SerializerSettings);
});
mvcBuilder.AddMvcOptions(o => { o.Filters.Add(new GlobalExceptionFilter()); });
...
The documentation is lacking, but you can see what JsonSerializationFns
does by looking in the Breeze code.
The last line adds an exception filter that wraps server-side validation errors so that the Breeze client can handle them. You can see what it does here.