5.5.0 to 5.5.x Entity Edit

Posts   
 
    
howes
User
Posts: 2
Joined: 16-Aug-2019
# Posted on: 16-Aug-2019 22:08:02   

Using the DTO template I grabbed from this forum I am using the ToEntity() Method to grab a JSON input from a Web Api input and convert that to an entity and then edit values in the parent and child of that entity.

DTO Template:

https://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=13256&HighLight=1

Since 5.5.1 and above when I try to edit the pk of the child (it is combination key), it will remove the entity from the graph. In the change log it appears that this was possibly addressed. (i.e pk changed entity might be out of sync, detach)


                var task = newTaskEditModel.ToEntity<TaskDTO, TaskEntity>(true, true);
                task.CustomNumber = _customNumberServices.GetNextCustomNumberLong(userDetails.TenantId, GetNextCustomNumberLongEnum.Task);
                task.CreateDate = task.UpdateDate = currentUtc;
                task.TableEntityTypeId = (short)EntityTypeEnum.Task;
                task.TenantId = userDetails.TenantId;
                if (!task.TaskStates.Any())
                {
                    task.TaskStates.Add(new TaskStateEntity
                    {
                        Description = string.Empty,
                        EntityStateTypeId = (int)TaskEntityStateTypeEnum.NotStarted
                    });
                }

                var taskState = task.TaskStates.Single();
                taskState.TenantId = userDetails.TenantId;

                //since TenantId is part of the pk the entity will be removed from graph

                taskState.CreateId = userDetails.UserId;
                taskState.CreateDate = currentUtc;
                taskState.TableEntityTypeId = (short)EntityTypeEnum.Task;

I have found that if I create a new child entity and then essentially clone it from the original child, then detach the original (detachFromGraph()), and then add the cloned one I get the expected results.

I have a bunch of code surrounding the editing of that pk and need some advice on what would be the suggestion solution, or if this is indeed a bug.

The combination primary key is basically for a multi-tenancy. pk = tenantId + pk of the entity.

Thanks for having a look,

Jon

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 17-Aug-2019 08:58:52   

Hi Jon,

Please help us figuring out some things to understand the problem:

  • Describe the relation between TaskStateEntity and TaskEntity. What is the TaksEntity PK btw?

  • Is this something that worked on v5.5.0 but now it doesn't? What exact runtime library version are you using right now?

  • What is then changelog entry you saw about this?

  • In the code you shared, if the taskState.TenantId field is part of the FK that points to TaskEntity PK, it shouldn't be necessary to set such field, because when you save the graph the PK will be propagated. Am I wrong?

David Elizondo | LLBLGen Support Team
howes
User
Posts: 2
Joined: 16-Aug-2019
# Posted on: 19-Aug-2019 20:11:25   

Thanks for getting back to me

Task --> TaskState relationship is a 1 to many relationship pks are taskStateID and tenantID and TaskId is the fk.

Editing the pk of a child entity (tenantId) WORKS in 5.5.0 and does not work in anything 5.5.1 and above

I may have been mistaken on the change log. It appears what I was referencing was for the Templates and not the runtime.

If I do not set the tenantId in the child entity and save it to the db. The child entity does pickup the new tenantId.

the tenantId is extremely important and that is why we were writing it. As I said, the code posted was working 100% in 5.5.0. When I upgrade the libs it will detach the child entity from the parent.

I am just trying to figure out was this designed behavior in 5.5.1 or was I just being overly cautious and there was no need to write the child pk (tenantId)

Best,

Jon

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 20-Aug-2019 00:40:10   

This is a designed behavior.

Please check FK-PK synchronization

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39613
Joined: 17-Aug-2003
# Posted on: 20-Aug-2019 09:30:49   

I think it's this change:

Fix Runtime Libraries 5.5 5.5.1 12-Dec-2018 When an FK field is re-used in multiple relationships and also is a PK field in another relationship, setting the FK field could cause a desync of the wrong related entities

Other than that, we didn't change anything (this fix was necessary as it was a bug).

Frans Bouma | Lead developer LLBLGen Pro