C
C#2d ago
Khebabchi

adding asp.net constaint on the class object field

in asp.net efcore :
c#
public class Consultation
{
[Key] // Primary Key
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] // Auto-increment
public int Id { get; set; }

[Required]
public DateOnly Date { get; set; } = DateOnly.FromDateTime(DateTime.UtcNow); // Default to current date

[Required]
public int MedecinId { get; set; } // Foreign Key

[Required]
public int PatientId { get; set; } // Foreign Key

// Navigation Properties
public User User { get; set; }
public Patient Patient { get; set; }
}
c#
public class Consultation
{
[Key] // Primary Key
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] // Auto-increment
public int Id { get; set; }

[Required]
public DateOnly Date { get; set; } = DateOnly.FromDateTime(DateTime.UtcNow); // Default to current date

[Required]
public int MedecinId { get; set; } // Foreign Key

[Required]
public int PatientId { get; set; } // Foreign Key

// Navigation Properties
public User User { get; set; }
public Patient Patient { get; set; }
}
how can i force that User.role == "Medecin" in the Consultation class when inserting
2 Replies
sibber
sibber2d ago
those first 2 attributes are the default wdym by force? it can only be that?
Khebabchi
KhebabchiOP2d ago
i will make it so only "Medecin" can create a consultation, so i want to add validation that when you create a consultation then the User.role must be "Medecin" (of corse i will do permissions for routes, but thats application level validation) .

Did you find this page helpful?