We have a database table called EntUser.
The table contains, among others, the fields: p_userID and p_isGroup.
The table contains our users and groups, and p_isGroup determines whether the entity is a user or group.
What users that are in what groups is determined in an second table called EntUserGroups.
This table has two fields: p_userID and p_groupID
Ideablade Entity model gives us the opportunity to see what groups a user is in by this statement:
p_currentUser.p_userGroups;
And what users that are in a group:
p_currentGroup.p_users;
p_currentUser and p_currentGroup are of type EntUser.
P_userGroups and p_users are of type IdeaBlade.EntityModel.RelatedEntityList<EntUser>
On my UI, I have two ListBoxes: one shows all groups and the second shows the groups that a current user is in. Between these ListBoxes are two buttons: Add and Remove.
When I add or remove a group to/from the current users groups, a Save button shall become enabled – this works fine by looking at the entitymanager.EntityChanged event.
The code behind the Add button looks something like this:
protected void OnAddGroup(EntUser newValue)
{
p_currentUser.p_userGroups.Add(newValue);
p_currentUser.EntityAspect.SetModified();
}
This works fine, but I have to use SetModified to trigger the entitymanager.EntityChanged event.
My question here: is this the right way to do this?
The code behind the Remove button looks something like this:
protected void OnRemoveGroup(EntUser newValue)
{
p_currentUser.p_userGroups.Remove(newValue);
p_currentUser.EntityAspect.SetModified();
}
This does not remove the group from p_userGroups. I get this exception when I save:
Entity Save Error: System.Data.OptimisticConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries. at System.Data.Mapping.Update.Internal.UpdateTranslator.ValidateRowsAffected(Int64 rowsAffected, UpdateCommand source) at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache) at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options) at IdeaBlade.EntityModel.Edm.EdmSaveExecutor.ProcessSaves(IEnumerable`1 groupsByType) at IdeaBlade.EntityModel.Edm.EdmSaveExecutor.SaveWithinContext() at IdeaBlade.EntityModel.Edm.EdmSaveExecutor.Save(DataSourceResolver dataSourceResolver, IDataSourceKey dsKey, SaveWorkState workState)
I then changed my Remove method to something like this:
protected void OnRemoveGroup(EntUser newValue)
{
var i = p_currentUser.p_userGroups.IndexOf(newValue);
p_currentUser.p_userGroups i .EntityAspect.Delete();
updateGroupIdsForUser();
}
But this actually deletes the group from the EntUser database table J
My question here: how do I remove a group from p_userGroups and save the changes?
Edited by danjal - 19-Nov-2010 at 2:28am