This article describes the possibility to have constructors in entity types as of Asp.Net Core v2.1.
And I thought, could it be possible to automatically create some related data upon creation of an entity without writing any code for it in the controller?
Something like this:
public class ProjectTask
{
public ProjectTask()
{
// Somehow check if we are creating a new ProjectTask.
// if (create)
// {
Notifications.Add(new ProjectTaskNotification
{
IsSent = false,
ProjectTaskId = Id,
Task = this,
TimeBefore = new TimeSpan(7, 0, 0, 0)
});
// }
}
public int Id { get; set; }
// Some more properties...
public Member TaskOwner { get; set; }
public List<ProjectTaskNotification> Notifications { get; set; }
public Project Project { get; set; }
}
And then I would simply save the ProjectTask-entity in the controller, without having to worry about Notifications:
db.Add(projectTask);
await db.SaveChangesAsync();
Can it be done? I don't know either C# or EF all that well, so maybe I'm missing something obviously basic?