dotnetnuke2sxc

In 2sxc, after an App.Data.Create(), is there a way to get the new EntityId (or Guid) or a Pointer to the new Entity?


I've run in to this need a few times in the past few years. I programmatically add a new Entity to a Content-Type using App.Data.Create() and then need to add that new Entity to the next Entity I am about to .Create (new record in a different Content-Type).

Since .Create() doesn't return anything, what is the best, correct, or elegant way to get to the new item?

Here is my janky solution, but it seems like there is probably a more logical way. Also on a busy site this might not work if there are multiple .Create()s at nearly the same time.

        // find the newest one with the highest EntityId 
        int maxEntityId = 0; 
        var tmp = AsList(App.Data["Markers"])
          .OrderByDescending(m => m.EntityId)
          .FirstOrDefault()
        ;
        if(tmp != null){
          maxEntityId = tmp.EntityId;
        }

Solution

  • Yes, there is :).

    var x = App.Data.Create(...);
    // X is now an a IEntity
    
    var dynX = AsDynamic(x);