chatrli
chatrli
CC#
Created by chatrli on 10/13/2023 in #help
❔ Relationship validation
nevermind i find the issue, i will use dto thanks a lot mate, have a great night 😃
32 replies
CC#
Created by chatrli on 10/13/2023 in #help
❔ Relationship validation
Here, if PersonId is nullable so PersonId? , the ide screams at me, asking to remove the getter and setter I need both
32 replies
CC#
Created by chatrli on 10/13/2023 in #help
❔ Relationship validation
Last question about the model if i may:
c#
public class Email
{
[Key]
public int Id { get; set; }
public string EmailAddress { get; set; }
public int PersonId { get; set; }
public virtual Person Person { get; set; }
}
c#
public class Email
{
[Key]
public int Id { get; set; }
public string EmailAddress { get; set; }
public int PersonId { get; set; }
public virtual Person Person { get; set; }
}
32 replies
CC#
Created by chatrli on 10/13/2023 in #help
❔ Relationship validation
anyway thanks amillion for that, just this ? mark make me struggling since yesterday
32 replies
CC#
Created by chatrli on 10/13/2023 in #help
❔ Relationship validation
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.
32 replies
CC#
Created by chatrli on 10/13/2023 in #help
❔ Relationship validation
thanks a lot mate, I have a new error but it might be something i can troubleshoot
32 replies
CC#
Created by chatrli on 10/13/2023 in #help
❔ Relationship validation
yes, after migrations its different
32 replies
CC#
Created by chatrli on 10/13/2023 in #help
❔ Relationship validation
I'm testing it right now mate
32 replies
CC#
Created by chatrli on 10/13/2023 in #help
❔ Relationship validation
So I have to add interrogation mark to both?
32 replies
CC#
Created by chatrli on 10/13/2023 in #help
❔ Relationship validation
DatabaseContext.cs
c#
using Microsoft.EntityFrameworkCore;

namespace api_os.Models
{
public class DatabaseContext : DbContext
{
public DatabaseContext()
{
}
public DatabaseContext(DbContextOptions<DatabaseContext> options)
: base(options)
{
}

protected override void OnConfiguring(DbContextOptionsBuilder options)
{
if (!options.IsConfigured)
{
options.UseSqlite("Data Source=database.db");
}
}

public DbSet<Person> People { get; set; }
public DbSet<Email> Emails { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Entity<Email>()
.HasOne(e => e.Person)
.WithMany(p => p.Emails)
.HasForeignKey(e => e.PersonId);
}
}
}
c#
using Microsoft.EntityFrameworkCore;

namespace api_os.Models
{
public class DatabaseContext : DbContext
{
public DatabaseContext()
{
}
public DatabaseContext(DbContextOptions<DatabaseContext> options)
: base(options)
{
}

protected override void OnConfiguring(DbContextOptionsBuilder options)
{
if (!options.IsConfigured)
{
options.UseSqlite("Data Source=database.db");
}
}

public DbSet<Person> People { get; set; }
public DbSet<Email> Emails { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Entity<Email>()
.HasOne(e => e.Person)
.WithMany(p => p.Emails)
.HasForeignKey(e => e.PersonId);
}
}
}
32 replies
CC#
Created by chatrli on 10/13/2023 in #help
❔ Relationship validation
Person.cs
c#
using System.ComponentModel.DataAnnotations;

namespace api_os.Models {
public class Person {
[Key]
public int Id { get; set; }
public string? FirstName { get; set; }
public string? MiddleName { get; set; }
public string? LastName { get; set; }
public string? BirthDate { get; set; }
public string? Nationality { get; set; }
public int? NationalId { get; set; }
public string? CurrentPlace { get; set; }
public string? CurrentPosition { get; set; }
public bool IsPubliclyWanted { get; set; }
public bool IsPrivatlyWanted { get; set; }
public bool IsDangerous { get; set; }
public ICollection<Email> Emails { get; set; }
}
}
c#
using System.ComponentModel.DataAnnotations;

namespace api_os.Models {
public class Person {
[Key]
public int Id { get; set; }
public string? FirstName { get; set; }
public string? MiddleName { get; set; }
public string? LastName { get; set; }
public string? BirthDate { get; set; }
public string? Nationality { get; set; }
public int? NationalId { get; set; }
public string? CurrentPlace { get; set; }
public string? CurrentPosition { get; set; }
public bool IsPubliclyWanted { get; set; }
public bool IsPrivatlyWanted { get; set; }
public bool IsDangerous { get; set; }
public ICollection<Email> Emails { get; set; }
}
}
Email.cs
c#
using System.ComponentModel.DataAnnotations;

namespace api_os.Models
{
public class Email
{
[Key]
public int Id { get; set; }
public string EmailAddress { get; set; }
public int PersonId { get; set; }
public virtual Person Person { get; set; }
}
}
c#
using System.ComponentModel.DataAnnotations;

namespace api_os.Models
{
public class Email
{
[Key]
public int Id { get; set; }
public string EmailAddress { get; set; }
public int PersonId { get; set; }
public virtual Person Person { get; set; }
}
}
32 replies
CC#
Created by chatrli on 10/13/2023 in #help
❔ Relationship validation
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)
32 replies
CC#
Created by chatrli on 10/13/2023 in #help
❔ Relationship validation
The model seems fine as I can see a null value when I get a record as seen below:
{"id":1,"firstName":"John","middleName":null,"lastName":"Doe","birthDate":"2000-01-01 00:00:00","nationality":"American","nationalId":null,"currentPlace":null,"currentPosition":null,"isPubliclyWanted":false,"isPrivatlyWanted":false,"isDangerous":false,"emails":null}
{"id":1,"firstName":"John","middleName":null,"lastName":"Doe","birthDate":"2000-01-01 00:00:00","nationality":"American","nationalId":null,"currentPlace":null,"currentPosition":null,"isPubliclyWanted":false,"isPrivatlyWanted":false,"isDangerous":false,"emails":null}
32 replies
CC#
Created by chatrli on 10/13/2023 in #help
❔ Relationship validation
Exactly
32 replies
CC#
Created by chatrli on 10/13/2023 in #help
❔ Relationship validation
is this possible to ask an api example if someone have one?
32 replies
CC#
Created by chatrli on 10/13/2023 in #help
❔ Relationship validation
But I didn't seet any validation here as I want to be able to leave blank field
32 replies
CC#
Created by chatrli on 10/13/2023 in #help
❔ Relationship validation
According to my knowledge, if im not wrong this is a validation issue
32 replies
CC#
Created by chatrli on 10/13/2023 in #help
❔ Relationship validation
c#
[HttpPost("{id}/emails")]
public ActionResult AddEmailToPerson(int id, Email email)
{
var person = _context.People.Find(id);
if (person == null)
{
return NotFound("Not found");
}

email.PersonId = id;
_context.Emails.Add(email);
_context.SaveChanges();

return Ok(email);
}
c#
[HttpPost("{id}/emails")]
public ActionResult AddEmailToPerson(int id, Email email)
{
var person = _context.People.Find(id);
if (person == null)
{
return NotFound("Not found");
}

email.PersonId = id;
_context.Emails.Add(email);
_context.SaveChanges();

return Ok(email);
}
cause error:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-5628679ef1a96e94cb3c5347f4417827-abfa0d432f9b4cbc-00",
"errors": {
"Person": [
"The Person field is required."
]
}
}
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-5628679ef1a96e94cb3c5347f4417827-abfa0d432f9b4cbc-00",
"errors": {
"Person": [
"The Person field is required."
]
}
}
32 replies
CC#
Created by chatrli on 10/13/2023 in #help
❔ Relationship validation
Hello thanks for the answer @V.EINA Jaken ! When I call the POST request for emails
32 replies
CC#
Created by chatrli on 10/13/2023 in #help
❔ Relationship validation
Just for the example, here a row of Person:
json
{"id":1,"firstName":"John","middleName":null,"lastName":"Doe","birthDate":"2000-01-01 00:00:00","nationality":"American","nationalId":null,"currentPlace":null,"currentPosition":null,"isPubliclyWanted":false,"isPrivatlyWanted":false,"isDangerous":false,"emails":null}
json
{"id":1,"firstName":"John","middleName":null,"lastName":"Doe","birthDate":"2000-01-01 00:00:00","nationality":"American","nationalId":null,"currentPlace":null,"currentPosition":null,"isPubliclyWanted":false,"isPrivatlyWanted":false,"isDangerous":false,"emails":null}
Imo the relationship is ok. Maybe my POST method is wrong as it should be simple to append a new element in an array
32 replies