❔ Relationship validation
Here is my controller, feel free to navigate through the repo: https://github.com/chatrli/api_onotebook/blob/emails/Controllers/PeopleController.cs
I added a relationship between my Person and Email models.
Then I wrote a POST method to add an new email to a person by its id but i always the same error using postman and curl (with this route http://localhost:5062/people/1/emails)
I don't need the person field to be required actually I want to be able to have Person with no email and just register new email later. There is no Person + Email creation the same time.
As I'm new to dotnet and have to be confortable with at least web api soon, I wish I can see how a simple relationship is used through the controller. Else if you have an explanation I will be glad.
What I tried so far is making the Person field nullable in the Post method for create email but in comparison of other backend Im working with so far, it feels like a non sense.
When I said it feels like a non sense I mean that this should not require to be nullable? We should be able to just add a new email without creating a new Person, as on client side, there is 2 form. One for the Person and a second one for the Email that is in Person Detail page.
GitHub
api_onotebook/Controllers/PeopleController.cs at emails · chatrli/a...
Contribute to chatrli/api_onotebook development by creating an account on GitHub.
17 Replies
Just for the example, here a row of Person:
Imo the relationship is ok. Maybe my POST method is wrong as it should be simple to append a new element in an array
where does this exception throw?
Hello thanks for the answer @V.EINA Jaken ! When I call the POST request for emails
let's see it
cause error:
According to my knowledge, if im not wrong this is a validation issue
But I didn't seet any validation here as I want to be able to leave blank field
is this possible to ask an api example if someone have one?
so, the exception throws at
_context.SaveChanges()
?Exactly
The model seems fine as I can see a null value when I get a record as seen below:
I believe the behavior is, I can't create a single email, i have to create a record(here person) and a email the same time.
But the behavior I expect and need to do is, create an email on top of a record(here person)
let's see the EF models, then
somehow, you've written them to make the
Person
relationship requiredPerson.cs
Email.cs
DatabaseContext.cs
neither
PersonId
nor Person
is nullableSo I have to add interrogation mark to both?
if you want it to be legal for them to be
null
, you have to specify that it's legal for them to be null
, yesI'm testing it right now mate
you'll need to rebuild your migrations, if you're using the designer
yes, after migrations its different
thanks a lot mate, I have a new error but it might be something i can troubleshoot
System.Text.Json.JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles. Path: $.Person.Emails.Person.Emails.Person.Emails.Person.Emails.Person.Emails.Person.Emails.Person.Emails.Person.Emails.Person.Emails.Person.Emails.Person.Id.
anyway thanks amillion for that, just this ? mark make me struggling since yesterday
Last question about the model if i may:
Here, if PersonId is nullable so PersonId? , the ide screams at me, asking to remove the getter and setter
I need both
nevermind i find the issue, i will use dto
thanks a lot mate, have a great night 😃
gg
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.