0

I am creating a new .NET Core application with the following components:

  1. .NET Core 3.0 Web API for apis
  2. .NET Core Razor pages for the web UI
  3. .NET Core class libraries - one for utilities and one for the model

I am using Entity Framework core for the model. I generally hate ORM tools but time is critical for this project so I don't have much choice. I have to say that, all things considered, it is pretty darn solid.

One of the issues I had is that I had to decorate some of my model's properties with the JsonIgnore attribute, e.g. :

[JsonIgnore]
public virtual ICollection<LabOrder> LabOrders { get; set; }

The reason for that is that otherwise, when I serialize the object (in order to return it from an api) .NET throws the exception about too many nested levels or whatever because of parent-child-back-to-parent relationships that would cause a never-ending recursion to happen. So, all fine and dandy, the JsonIgnore attribute did just what I need.

BUT, here is my issue. I've gone through the trouble of decorating these properties everywhere I need to, and completed a first round of development and testing. I need to make some adjustments to my database model; which I've done... and now it is time to refresh the EF Core dbcontext. When creating new properties or methods I can use partial classes to retain any "custom" changes I make to the model, but I have no idea how I might accomplish that here.

So: Is it possible for one to retain my property decorations and still use the Scaffold-dbcontext command to refresh the model? I've used that command successfully in the past, but now I need to make sure I somehow don't lose my changes.

Buckles
  • 161
  • 1
  • 9
  • If you need to custom decorate your models you might be better off with a code-first approach instead of database first? Or separate your front end classes from your data classes – Carl Nov 19 '19 at 14:08
  • That is a good point around the code-first approach. I guess I am old-school so I prefer to stick with db-first. I could create another layer on top of the EF model I suppose - although at that point I kind of feel like I'm losing some of the benefit of using EF to begin with. That is a good thought, though. Thank you! – Buckles Nov 19 '19 at 14:22
  • Hi there Buckles and welcome to SO. One hacky solution is to have a separate project that connects only to the database and there you do the scaffolding command and manually copy over your new fields. – Archlight Nov 19 '19 at 14:37
  • This should help https://stackoverflow.com/questions/6550409/how-to-add-attributes-to-a-base-classs-properties – EJD Nov 19 '19 at 18:11
  • Thanks EJD. I thought that looked like just what I needed, but I tried it and unfortunately it isn't respecting the [JsonIgnore] annotations in the MetadataType class. – Buckles Nov 19 '19 at 21:45
  • So it looks like core implements this differently and I need to use Microsoft.AspnetCore.Mvc.ModelMetadataType instead. I'm going to try that but I'm kinda bummed because, until now, this has been my API only project and therefore hasn't needed the core mvc package installed. Bummer! – Buckles Nov 19 '19 at 21:49
  • And..... that seems to be completely ignored by .net core. Lots of talk about it: https://stackoverflow.com/questions/34576921/asp-net-core-metadatatype-%20attribute-not-working I'm trying some of the hacky things people are suggesting, but I sure this thing actually worked! – Buckles Nov 19 '19 at 22:19
  • Interesting stuff I’m about to do something similar soon. – EJD Nov 20 '19 at 03:04
  • Yeah unfortunately nothing about ModelMetadataType works the way it claims. Its just straight up ignored and apparently its a known issue: https://github.com/JamesNK/Newtonsoft.Json/issues/1349 I think at this point my best option is to create a ViewModel and have my APIs just use that. Kind of a pain in the arse, but it is what it is. I've always thought people over-do it with the # of layers they create for their model (base model, DTO layer, viewmodel layer, etc.) - but I guess this is a perfect example of how my previous line of thinking is short-sighted. Time to learn AutoMapper – Buckles Nov 20 '19 at 13:27
  • @Buckles Is it an option to adjust the code generation as described on https://stackoverflow.com/questions/37679367/entity-framework-core-customise-scaffolding? – Progman Nov 20 '19 at 19:06

1 Answers1

0

I believe I have determined that this is in fact, simply not doable.

https://forums.asp.net/t/2139531.aspx?Loading%20MetaData%20from%20Partial%20Class%20in%20Aspnet%20Core

I've read through everything I can find here, the forums, and the MS documentation. It should work, but the decorations simply aren't respected when in a ModelMetadataClass. The only way to do this is to implement another layer such as a view-model and handle it all there.

Buckles
  • 161
  • 1
  • 9