我正在尝试以 xamarin 形式访问外键(所有者),但它只返回 null
。根据堆栈中的许多其他问题,我似乎必须使用延迟加载或急切加载来从外键获取数据。
由于延迟加载尚未在 Core 中实现,我尝试使用预加载,但我仍然在 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();
}
我的 Controller :
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)
之外,我还需要其他东西来让它工作吗?
最佳答案
我在您的建筑模型中没有看到外键属性,只有导航属性。
我认为你应该包括:
public int OwnerId { get; set; }
在建筑类中。
关于c# - EF 核心 : Foreign key returns null value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43709108/