RxNsx
RxNsx

Reputation: 5

How to insert an item into the collection with ef core

It's a class where i need to add item in Subtasks with Ef Core. Have no idea how to do it. Please help!

public class Do
    {
        [Key]
        public int Id { get; set; }
        [Required]
        public string Title { get; set; }
        [Required]
        public string Description { get; set; }
        public string Executors { get; set; }
        public DoStatus Status { get; set; }
        public DateTime Created { get; set; } = DateTime.Now;
        [Required]
        public int Plan { get; set; }
        public int Fact { get; set; }
        public DateTime? Finished { get; set; }
        public virtual ICollection<Do> SubTasks { get; set; } = new List<Do>();
    }

Upvotes: 0

Views: 165

Answers (1)

user19326394
user19326394

Reputation:

you need to learn about the recursive CTE relationship for the date configure there with Api

builder.Property(p => p.Created).HasDefaultValueSql("(newid())")

public class Task
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string Title { get; set; }
    [Required]
    public string Description { get; set; }
    public string Executors { get; set; }
    public DoStatus Status { get; set; }
    public DateTime Created { get; set; };
    [Required]
    public int Plan { get; set; }
    public int Fact { get; set; }
    public DateTime? Finished { get; set; }
    public int DoId { get; set; } // id parent
    public virtual List<Task> SubTasks { get; set; };
}

var id = 1 // id Do

var subTasks = db.Task.include(s => s.SubTasks).where(s => e.DoId = id).ToList()

Upvotes: 1

Related Questions