Please help a probably really dumb guy (API)
Hey everyone, I'm currently doing an API and I'm pretty sure I'm doing something bad : I have MANY classes, that I designed to be use as an API and for code-first usage (school project, we had no choice). For example, here is the Couleur class :
It's one of the tiniest classes among the project. In order to post a new couleur, I would like to just pass the following json :
BUT : when i try to do so, i get this error :
So I added the VariantesCouleurProduitCouleur, which is the navigation property between Couleur and VariantesCouleurProduit ;
and that worked.
5 Replies
As this class is clearly the simpliest one, I'm afraid I'll need to set a null property for EACH navigation property included in EACH class. Do you know if that's a classic behavior, or did I full mistaken my code in the whole project ? Ty for your answer !
your EF models shouldn't be anywhere near JSON
the database model is for the database only, you should have different models for your API requests and responses
then you map the API models to/from the database models in code
otherwise you get issues like this where the database model has different properties than you want in your API
I'm not sure to understand, what changes should I make exactly ? Because the database is generated in code-first, so by using API's models. Here again, I'm surely misunderstanding how the whole thing works.
you have
API request -> DB model -> database
you should have
API request -> API model -> DB model -> database
ok, I'll try to do that, ty