使用迁移ASP.NET获取对象的FK信息



我有一个实体,它与其他人有关联,处于ONE-TO-MANY关系中。我使用迁移来生成表。

我的实体:

[Key]
public int Id { get; set; }
public string Name { get; set; }
public List<ProfessionalInformation> ProfessionalInformations { get; set; }
public List<Language> Languages { get; set; }

当我查询实体时,她不会向我返回相关对象的信息。

public IEnumerable<PersonalInformation> Get(Expression<Func<PersonalInformation, bool>> predicate)
{
return _context.PersonalInformations.Where(predicate);
}

仅返回此信息:

{
"id": 4,
"name": "David Pires"
}

我想让该查询也返回关联实体的信息,我该怎么做?

这是我的DbContext:

public DbSet<ProfessionalInformation> ProfessionalInformations { get; set; }
public DbSet<PersonalInformation> PersonalInformations { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
foreach (var relationship in builder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
{
relationship.DeleteBehavior = DeleteBehavior.Cascade;
}
base.OnModelCreating(builder);
}

感谢

我已经找到了一个解决方案:https://learn.microsoft.com/en-us/ef/ef6/querying/related-data感谢

最新更新