Are navigation added automatically to the dbSet?

Mahesh Kumar 65 Reputation points
2024-12-27T08:15:14.9233333+00:00

I have a question Dbset

public class Question {
public int Id {get; set;} 
public string Text {get; set;} 
public virtual ICollection<Choice> Choices {get; set;} = []

Choice DbSet/Model

public class Choice
{
    public int Id { get; set; }
    
    [Required]
    [MaxLength(100)]
    public string Text { get; set; } = string.Empty;
    
    [Required]
    public int QuestionId { get; set; }

    
    [JsonIgnore]
    public virtual Question Question { get; set; }
}

I am sending data from angular.

{
   text: "dfdfdfd",
   choices: [
      { text: "choice1"},
      { text: "choice2"}
   ]
}

Now if i add it to the dbset, in the following way,

_context.Questions.Add(Question);
        await _context.SaveChangesAsync();

It is adding only the questions to the Dbset Why are the choices being ignored?

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
768 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,550 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.