【发布时间】:2017-05-01 02:44:58
【问题描述】:
我正在尝试以 xamarin 形式访问外键(所有者),但它只返回 null。根据堆栈中的许多其他问题,我似乎必须使用延迟加载或急切加载来从外键获取数据。
由于延迟加载尚未在 Core 中实现,我尝试使用 Eager Loading 代替,但我仍然在 xamarin 表单和后端获得 null 值。
我的模特:
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; }
}
我的仓库:
public IEnumerable<Building> GetAll()
{
return _context.buildings.Include(b => b.Owner).ToList();
}
我的控制器:
public IEnumerable<Building> Get(Building value)
{
return _BuildingRepository.GetAll();
}
我的 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);
}
}
除了我的存储库中的 Include(b => b.Owner) 之外,我还需要其他的东西来让它工作吗?
【问题讨论】:
-
建筑模型中有业主吗??
-
是的,如果您是这个意思,所有者是建筑模型中的 ApplicationUser
-
需要更多代码。关系是如何建立的?像这样的获取所有方法会将所有建筑物和所有者加载到内存中。顺便说一句,这很糟糕。有更专业的查询。但好吧,这不是代码审查! :p
-
@ebk 能否包含您的上下文文件,确保数据在数据库中,并包含显示您如何保存文件的方法。
-
你需要在 Building 中包含 OwnerId 属性
标签: c# asp.net entity-framework xamarin