Let me explain my situation a little. Every table in my database has an UpdateBy and CreateBy column, FKs to the User table. This includes the User table with FKs to itself.
Whenever an new entity is persisted, I should set CreateBy to the currently logged in user. On all saves, UpdateBy should be set to the logged in user.
I'm curious what the best way to automate this is. Right now I have derived my own DataAccessAdapter and overriden the SaveEntity() method:
public override bool SaveEntity(IEntity2 entityToSave, bool refetchAfterSave,
IPredicateExpression updateRestriction, bool recurse)
{
PrepEntity(entityToSave);
return base.SaveEntity(entityToSave, refetchAfterSave, updateRestriction, recurse);
}
Which calls PrepEntity (_loggedInUser is a UserEntity object):
private void PrepEntity(IEntity2 entity)
{
if (!entity.IsDirty && !entity.IsNew)
return;
for (int i = 0; i < entity.Fields.Count; i++)
{
IEntityField2 f = (IEntityField2) entity.Fields[i];
if (f.Name == "UpdateBy" || f.Name == "CreateBy" && entity.IsNew)
f.CurrentValue = _loggedInUser.Id;
}
}
The current problem I have is that the _loggedInUser must be a UserEntity that has an Id already. I can't add a UserEntity and also some other row in another table all in one shot. I have to save the user first, then set _loggedInUser to a UserEntity with an Id, then do the rest of my saves.
I could get around this if I set the related entity property instead of the UpdateBy/CreateBy int columns directly, then LLBLGen could handle the dependencies for me... but I don't know how to do this dynamically. I suppose if I named all of the relationships "UserUpdateBy" and "UserCreateBy" and used reflection on the resulting properties, I could set them... but that pretty dirty
I could call IEntity2's SetRelatedEntity directly, but it looks like the properties have logic that I can't possibly know about (look at the generated code for the setter properties for related entities).
Other ways I can think of would require changes to the entities, which I don't want to do. This should all be automatic for me
Any ideas?