У меня следующий контекст базы данных приложения:
public class ApplicationContext : DbContext
{
private readonly string _connectionString;
public ApplicationContext(IConfiguration configuration)
{
_connectionString = configuration.GetConnectionString("Recipes");
}
public DbSet<User> Users { get; set; }
public DbSet<Recipe> Recipes { get; set; }
public DbSet<Ingredient> Ingridients { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(_connectionString);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Ingredient>()
.HasOne(x => x.Recipe)
.WithMany(y => y.Ingredients);
}
}
Моя бизнес-модель проста: в одном рецепте много ингредиентов, а в одном ингредиенте только одна квитанция.
Модели
Рецепт:
public class Recipe
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public List<Ingredient> Ingredients { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
public Recipe()
{
CreatedAt = DateTime.UtcNow;
UpdatedAt = DateTime.UtcNow;
}
}
Ингредиент
public class Ingredient
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int ClientId { get; set; }
public string Name { get; set; }
public decimal Count { get; set; }
public string Measure { get; set; }
public int Number { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
public Recipe Recipe { get; set; }
public Ingredient()
{
CreatedAt = DateTime.UtcNow;
UpdatedAt = DateTime.UtcNow;
}
}
И здесь я пытаюсь получить коллекцию ингредиентов из рецепта:
List<Recipe> recipes;
recipes = _applicationContext.Recipes.Where(r => r.Category.ClientId == id).ToList();
Но ингредиенты всегданоль.Я не понимаю почему.Вот результат:
![enter image description here](https://i.stack.imgur.com/U36NK.png)
Что не так?