I'm working on a semi-enterprise WPF Application, using EF6+ (any version is okay, I can update it any time I like).
I already have the database set-up (from previous project, it's a MSSQL db), and I'm doing code first obviously. I'll use mock objects due to NDA.
I would like to know what to do in order for EF to inject Son
object from the database into the Man
object as marked by Foreign Key. e.g.:
[Table("Man")]
public class Man : ObservableObject
{
private int _id;
private int _sonId;
private Son _son;
public int Id
{
get => _id;
set => Set(() => Id, ref _id, value);
}
[ForeignKey("SonId")]
public Son Son
{
get => _son;
set => Set(() => Son, ref _son, value);
}
public int SonId
{
get => _sonId;
set => Set(() => SonId, ref _sonId, value);
}
}
[Table("Son")]
public class Son : ObservableObject
{
private int _id;
public int Id
{
get => _id;
set => Set(() => Id, ref _id, value);
}
}
I properly retrieve (using their respective repositories) objects from the database, however the Foreign Key object (Son
) is not mapped (it's null). Is this not an option in EF or I'm doing something wrong?
Repository
public async Task<IEnumerable<Man>> GetAll()
{
using (var ctx = new DatabaseContext())
{
return await ctx.Men.ToListAsync();
}
}
I did not use any [Key]
annotation, because I assume EF associates Id field as a primary key since my table PK is Id
, and I have some validation in other classes like [StringLength]
and [Range(1,10)]
, etc... Which I assume is not relevant for this problem.
Aucun commentaire:
Enregistrer un commentaire