In my app I have a BaseEntity model that is inherited by all other models, it looks like this.
public class BaseEntity{ public int Id { get; set; } public bool IsDeleted { get; set; } = false; public DateTime DateCreated { get; set; } = DateTime.Now; public DateTime? DateUpdated { get; set; } public DateTime? DateDeleted { get; set; }}In a given model I'm doing something like this.
public class TagVariant : BaseEntity{ public string Name { get; set; } = string.Empty; public int? TagId { get; set; } public Tag? Tag { get; set; }}public class Tag : BaseEntity{ public string Text { get; set; } = string.Empty; [JsonIgnore] public List<TagVariant> Variants { get; set; } = new List<TagVariant>();}My controller looks like this.
[HttpPost]public async Task<ActionResult<List<Tag>>> CreateTag(Tag tag){ //temp force to be false tag.IsDeleted = false; _dataContext.Tags.Add(tag); await _dataContext.SaveChangesAsync(); return Ok(tag);}As you can see I'm having to manually tell the submission to change isDeleted to be false. I have it set in the model, so why am I having to manually set it?
Thanks!