'Creating objects in ASP.NET Core 6 with EF Core, when they contain foreign keys
I'm a beginner with .NET and I'm having a hard time understanding how to create objects with foreign keys.
Let's say I have this Localitate entity (which vaguely translates to city) which contains a FK to Judet (county):
namespace xx.DataLayer.Entities
{
public class Localitate : BaseEntity
{
[Required]
public string Nume { get; set; }
[Required]
public int JudetID { get; set; }
[Required]
public virtual Judet Judet { get; set; }
public Localitate() { }
}
}
Here's how my request looks:
public class LocalitateRequest
{
public string Nume { get; set; }
public int JudetID { get; set; }
}
When I create a Localitate object, how should I do it? Do I need to fill the navigation property too, or is the int JudetID enough?
If it is enough, I'm encountering another problem - if I insert a JudetID that doesn't exist, I don't get any error (and I think I should, my DB would do that if I would be doing an insert statement).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
