1

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.

TheITDejan
  • 808
  • 9
  • 27
  • Try `ctx.Men.Include(m => m.Son).ToListAsync();`. See [here](https://msdn.microsoft.com/en-us/library/gg671236%28v=vs.103%29.aspx#Anchor_1). Also, since you prefer to skip the [Key] annotations you can also skip [Table] when it is the same name as the class and [ForeignKey] when it is the nav name + Id. https://msdn.microsoft.com/en-us/data/jj679962 – Steve Greene Nov 01 '17 at 13:18
  • Thanks a lot for the response, and yes this was my solution. Thanks for additional links :) I'll check it out. Can you respond as an answer so I can mark it? – TheITDejan Nov 01 '17 at 13:46
  • Also, is there a way to mark several objects to be injected at once instead of adding several .Include().Include().Include() ? – TheITDejan Nov 01 '17 at 13:47

1 Answers1

1

With EF6 you can use Include to eager load your child objects. I am not aware of a way to load multiple children without chaining Includes. You could create an extension method for loading all the children as shown here. Also note there are some changes with EF Core.

BTW, your class could be simplified if you are not altering the backing fields:

public class Man : ObservableObject  // ToTable Man by default
{
    public int Id { get; set; } //PK by convention

    public int SonId { get; set; } // FK by convention
    public Son Son { get; set; } 
}
Steve Greene
  • 12,029
  • 1
  • 33
  • 54