rivantsov / vita

VITA Application Framework
MIT License
59 stars 15 forks source link

[Bug] Entity is not refreshed after the child is being deleted #199

Closed jasonlaw closed 3 years ago

jasonlaw commented 3 years ago

Hi @rivantsov ,

I have the entity model schema as following, where a Poll can have multiple PollOption, and each PollOption can have multiple PollVoter.

In my case, when I retracted the vote by deleting the PollVoter from the PollOption, the record is successfully removed from DB, and the deleted PollVoter entity is marked as phantom.

In this case, when I try to check if I have any voter for the Poll or PollOption, I would expect no more voter since the only voter has been deleted. However, the linq query still returning me the phantom entity and the system would thought there is still a voter there, which is wrong.

I have tried EntityHelper.Refresh but still the same problem. I have also tried to query back the Poll entity by using EntitySet, again still the same problem.

Is this a bug? Otherwise, any way to reset the entity?

[Entity]
public interface IPoll : ICommunityTable
{
    [PrimaryKey, AutoID] string Id { get; }
    [Unlimited] string Question { get; set; }
    IList<IPollOption> Options { get; }
    DateTime? StartOn { get; set; }
    DateTime EndOn { get; set; }
    PollMode PollMode { get; set; }
    bool IsImmediateResult { get; set; }
    bool IsRetractable { get; set; }
    [Auto(AutoType.CreatedBy), Size(nameof(IAuth.LoginId))] string CreatedBy { get; }
    [Auto(AutoType.CreatedOn), Utc] DateTime CreatedOn { get; }
    [Auto(AutoType.UpdatedBy), Size(nameof(IAuth.LoginId))] string UpdatedBy { get; }
    [Auto(AutoType.UpdatedOn), Utc] DateTime UpdatedOn { get; }
}

[Entity]
public interface IPollOption
{
    [PrimaryKey, AutoID] string Id { get; }
    [CascadeDelete] IPoll Poll { get; set; }
    IList<IPollVoter> Voters { get; }
    [Size(500)] string Description { get; set; }
    [Auto(AutoType.UpdatedBy), Size(nameof(IAuth.LoginId))] string UpdatedBy { get; }
    [Auto(AutoType.UpdatedOn), Utc] DateTime UpdatedOn { get; }
}

[Entity]
public interface IPollVoter
{
    [PrimaryKey, AutoID] string Id { get; }
    [CascadeDelete] IPollOption PollOption { get; set; }
    IMemberInCommunity Member { get; set; }
    [Nullable] IUnit Unit { get; set; }
    [Auto(AutoType.CreatedOn), Utc] DateTime CreatedOn { get; }
}

public enum PollMode
{
    Open,
    Unit,
    Owner               
}

Net_EntityNotRefresh

rivantsov commented 3 years ago

yes, that's all looks like it behaves as expected. The deleted Voter entity, although marked as Fantom, is still stuck in the internal ref lists (poll.Voters). This is a compromise, to provide you with convenient auto-loaded lists of linked entities, but at the same time, when you delete an entity, the system does NOT run thru all loaded entities and internal lists removing it. In the case like yours I think the best approach is to build response from a new session. Like this: delete entity; session.SaveChanges; abandon session, open session2, retrieve entity by Id, build the response etc.

jasonlaw commented 3 years ago

Thanks for the clarification and suggested solution!