Home > Software design >  .Net 6 Code first json collection prop error
.Net 6 Code first json collection prop error

Time:04-09

I'm trying to make a code first application. And I created the entities below. The relationship I want is: A user can have multiple homes and have multiple questions. When trying to add a user, I leave the question and home ICollection properties blank, because I don't have those properties at the moment. But when I leave it blank, it gives validation error. where am i doing wrong

public abstract class BaseEntity
{
    public Guid Id { get; set; }
    public DateTime CreatedTime { get; set; }
    public DateTime ModifiedTime { get; set; }
    public bool IsActive { get; set; }
}

public class User : BaseEntity
{
    public string Email { get; set; }
    public string Password { get; set; }
    public DateTime BirthDate { get; set; }
    public bool IsHomeOwner { get; set; }
    public ICollection<Home> Homes { get; set; }
    public ICollection<Question> Questions { get; set; }
}

public class Question : BaseEntity
{
    public string QuestionBody { get; set; }
    public string QuestionAnswer { get; set; }
    public ICollection<User> Users { get; set; }
}

public class Home : BaseEntity
{
    public string Adress { get; set; }
    public Guid UserId { get; set; }
    public Guid HomePropertyId { get; set; }
    
    [ForeignKey("UserId")]
    public User User { get; set; }
    [ForeignKey("HomePropertyId")]
    public HomeProperty HomeProperties { get; set; }
}

public class HomeProperty : BaseEntity
{
    public string PropertyName { get; set; }
    public bool TV { get; set; }
    public bool Wifi { get; set; }
    public bool Cigarette { get; set; }
    public bool Alcohol { get; set; }
    public bool AllowGuest { get; set; }
    public short PeopleNumber { get; set; }
    public bool HasInvoice { get; set; }
    public short NumberOfRoom { get; set; }
    public Home Home { get; set; }
}

My User request body

{
  "id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
  "createdTime": "2022-04-08T13:13:50.470Z",
  "modifiedTime": "2022-04-08T13:13:50.470Z",
  "isActive": true,
  "email": "string",
  "password": "string",
  "birthDate": "2022-04-08T13:13:50.470Z",
  "isHomeOwner": true
}

I got this error

 {
  "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "traceId": "00-d757552c4af46b09997346664a695d46-00b9a78a6116b618-00",
  "errors": {
    "Homes": [
      "The Homes field is required."
    ],
    "Questions": [
      "The Questions field is required."
    ]
  }
}

CodePudding user response:

ASP.NET Core treats non-nullable reference types (see nullable reference types ) as required. You can either disable nullable reference types in .csproj:

<Nullable>disable</Nullable>

Though in general I would not recommend it. Or you can mark corresponding properties as nullable:

public class User : BaseEntity
{
    public string Email { get; set; }
    public string Password { get; set; }
    public DateTime BirthDate { get; set; }
    public bool IsHomeOwner { get; set; }
    public ICollection<Home> Homes { get; set; }
    public ICollection<Question> Questions { get; set; }
}

But I think better approach would be to create a DTO/Model which will not have these properties and use it in your controllers:

public class UserRequest : BaseEntity
{
    public string Email { get; set; }
    public string Password { get; set; }
    public DateTime BirthDate { get; set; }
    public bool IsHomeOwner { get; set; }
}

CodePudding user response:

Actually your request object should be different object from your entity object. Request object which user sends when making requests is named as DTO and contains only necessity fields for making request. But if you don't want to seperate request objects and entity objects, you can use following:

public class User : BaseEntity
{
    public string Email { get; set; }
    public string Password { get; set; }
    public DateTime BirthDate { get; set; }
    public bool IsHomeOwner { get; set; }
    public ICollection<Home?>? Homes { get; set; }
    public ICollection<Question> Questions { get; set; }
}

Making Homes field nullable as nullable object, will solve your error. All nullablefields should be declared nullable as be in Homes example.

  • Related