I want to be able to achieve something like:
public class EntityWithCollection { public int Id { get; set; } [NotMapped] public DependentEntity { get { return Dependencies?.SingleOrDefault(d => d.IsActive); } } [JsonIgnore]
[InverseProperty("Parent")] public ICollection<DependentEntity> Dependencies { get; set; } } public class DependentEntity { public int Id { get; set; } public string Name { get; set; } [JsonIgnore] public bool IsActive { get; set; }
public int ParentId { get; set; }
[ForeignKey("ParentId")]
public EntityWithCollection Parent { get; set; } }
But without making circular reference, i.e. additional column for DependentEntityId, this works for GET but doesn't work for POST,PUT anything that writes
So from my Web API I can POST JSON like:
POST /entitiesWithCollection {"dependentEntity": { "name": "Chuck Norris" } }
there can be only one IsActive=1 row for a given child. For that I have UNIQUE constraint:
builder.Entity<DependentEntity>().HasIndex(e => e.ParentId).HasFilter("IsActive=1");
I also have
builder.Entity<DependentEntity>().HasQueryFilter(e => e.IsActive);
How can I make this work so I can UPDATE and INSERT using [NotMapped] property?