Reputation: 107
I'm trying to access a foreign key (owner) in xamarin forms, but it only returns null
. According to many other questions here at stack it seems that I have to use lazy loading or Eager loading to get data from foreign key.
As lazy loading is not implemented in Core yet, I have tried to use Eager Loading instead, but I still get the null
value both in xamarin forms and on backend.
My Model:
public class Building
{
public int Id { get; set; }
public string BuildingName { get; set; }
public string Address { get; set; }
public int BuildingYear { get; set; }
public bool BuildingBool { get; set; }
public ApplicationUser Owner { get; set; }
}
My Repository:
public IEnumerable<Building> GetAll()
{
return _context.buildings.Include(b => b.Owner).ToList();
}
My Controller:
public IEnumerable<Building> Get(Building value)
{
return _BuildingRepository.GetAll();
}
My DbContext:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<Building> buildings { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
This is how the data looks like in the database:
Do I need something else than the Include(b => b.Owner)
in my repository to get it to work?
Upvotes: 3
Views: 2867
Reputation: 8625
I don't see foreign key property in your Building model, only navigation property.
I think you should include:
public int OwnerId { get; set; }
in Building class.
Upvotes: 2