Pedro
Pedro
CC#
Created by Pedro on 8/28/2023 in #help
❔ problem with DateTime format
guys, the "[DisplayFormat(DataFormatString = "{dd/MM/yyyy}")]" is not working for me: when i add an event or update it, still shows like this { "title": "string", "description": "string", "date": "2023-08-28T22:43:42.452Z", "attendees": [ "string" ] } can somebody help me?
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Net;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Identity;



namespace EventEnroll.Models
{
public class Event
{
[Key]
public int EventId { get; set; }
[Required]
[MinLength(8, ErrorMessage = "Title must be at least 8 characters.")]
public string Title { get; set; } = string.Empty;

[MaxLength(50, ErrorMessage = "Description cannot exceed 50 characters.")]
public string Description { get; set; } = string.Empty;

[Required]
[DisplayFormat(DataFormatString = "{dd/MM/yyyy}")]
public DateTime Date { get; set; }
public string? CreatorId { get; set; }
[ForeignKey("CreatorId")]
public IdentityUser? Creator { get; set; }
// Navigation properties
public ICollection<IdentityUser>? Attendees { get; set; } = new List<IdentityUser>();

}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Net;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Identity;



namespace EventEnroll.Models
{
public class Event
{
[Key]
public int EventId { get; set; }
[Required]
[MinLength(8, ErrorMessage = "Title must be at least 8 characters.")]
public string Title { get; set; } = string.Empty;

[MaxLength(50, ErrorMessage = "Description cannot exceed 50 characters.")]
public string Description { get; set; } = string.Empty;

[Required]
[DisplayFormat(DataFormatString = "{dd/MM/yyyy}")]
public DateTime Date { get; set; }
public string? CreatorId { get; set; }
[ForeignKey("CreatorId")]
public IdentityUser? Creator { get; set; }
// Navigation properties
public ICollection<IdentityUser>? Attendees { get; set; } = new List<IdentityUser>();

}
}
42 replies
CC#
Created by Pedro on 8/25/2023 in #help
❔ EntityFramework help determining relationship
System.InvalidOperationException: Unable to determine the relationship represented by navigation 'Event.Planner' of type 'User'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'. User.cs
using Microsoft.AspNetCore.Identity;

namespace EventEnroll.Models
{
public class User : IdentityUser
{
public List<Event> Events { get; set; } = new List<Event>();
}
}
using Microsoft.AspNetCore.Identity;

namespace EventEnroll.Models
{
public class User : IdentityUser
{
public List<Event> Events { get; set; } = new List<Event>();
}
}
Event.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Identity;

namespace EventEnroll.Models
{
public class Event
{
public int Id { get; set; }

[Required]
[MinLength(8, ErrorMessage = "Title must be at least 8 characters.")]
public string Title { get; set; } = string.Empty;

[MaxLength(50, ErrorMessage = "Description cannot exceed 50 characters.")]
public string Description { get; set; } = string.Empty;

[Required]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime Date { get; set; }

[Required(ErrorMessage = "Planner is required.")]
public User Planner { get; set; }

[Required(ErrorMessage = "At least one participant is required.")]
public List<User> Users { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Identity;

namespace EventEnroll.Models
{
public class Event
{
public int Id { get; set; }

[Required]
[MinLength(8, ErrorMessage = "Title must be at least 8 characters.")]
public string Title { get; set; } = string.Empty;

[MaxLength(50, ErrorMessage = "Description cannot exceed 50 characters.")]
public string Description { get; set; } = string.Empty;

[Required]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime Date { get; set; }

[Required(ErrorMessage = "Planner is required.")]
public User Planner { get; set; }

[Required(ErrorMessage = "At least one participant is required.")]
public List<User> Users { get; set; }
}
}
20 replies