我是CSLA.Net的新手,我很难从列表中删除子对象。
我有 3 节课:
Scenario : BusinessBase<Scenario>
ScenarioPermissions : BusinessListBase<ScenarioPermissions, ScenarioPermission>
ScenarioPermission : BusinessBase<ScenarioPermission>
类Scenario
包含一个类型的字段ScenarioPermissions
。
当我尝试做Scenario.Permissions.Add(obj)
andScenario.Save()
时,它可以正常工作。
但是如果我想做Scenario.Permissions.Remove(obj)
and ,它会继续而不从数据库中Scenario.Save()
删除我的对象。ScenarioPermission
public class Scenario : BusinessBase<Scenario>, IMakeCopy
{
private static readonly PropertyInfo<ScenarioPermissions> m_PermissionsProperty = RegisterProperty<ScenarioPermissions>(c => c.m_Permissions);
public ScenarioPermissions m_Permissions
{
get
{
if (!FieldManager.FieldExists(m_PermissionsProperty))
{
SetProperty(m_PermissionsProperty, ScenarioPermissions.NewPermissions());
}
return GetProperty(m_PermissionsProperty);
}
}
public ReadOnlyCollection<Model.Users.User> Permissions
{
get
{
var collection = new List<Model.Users.User>();
foreach (var item in m_Permissions)
{
collection.Add(Model.Users.User.GetUser(item.UserID));
}
//Adds administrators users...
var admins = Users.Users.GetUsers(true).Where(u => u.Role.InvariantName == "SystemAdministrator" || u.Role.InvariantName == "SuperAdministrator");
foreach (var item in admins)
{
collection.Add(item);
}
return new ReadOnlyCollection<Model.Users.User>(collection.OrderBy(u => u.FullName).ToArray());
}
}
public void AddPermission(Model.Users.User user)
{
if (user == null)
{
throw new ArgumentNullException("user");
}
ScenarioPermission permission = ScenarioPermission.NewPermission(this, user);
if (!this.m_Permissions.Contains(permission))
{
this.m_Permissions.Add(permission);
}
}
public void RemovePermission(Model.Users.User user)
{
if (user == null)
{
throw new ArgumentNullException("user");
}
ScenarioPermission permission = this.m_Permissions.FirstOrDefault(p => p.UserID == user.Id);
if (permission != null)
{
this.m_Permissions.Remove(permission);
}
}
protected override void DataPortal_Update()
{
using (var ctx = DbContextManager<DatabaseContext>.GetManager())
{
var context = ctx.DbContext;
var scenarioId = ReadProperty(m_IdProperty);
var scenario = context.Scenarios.FirstOrDefault(s => s.Id == scenarioId);
if (scenario != null)
{
scenario.Name = ReadProperty(m_NameProperty);
//Some codes....
context.SaveChanges();
}
FieldManager.UpdateChildren(this);
}
}
protected override void DataPortal_DeleteSelf()
{
DataPortal_Delete(ReadProperty(m_IdProperty));
}
private void DataPortal_Delete(Guid id)
{
using (var contextManager = DbContextManager<DatabaseContext>.GetManager())
{
var context = contextManager.DbContext;
var scenario = context.Scenarios.FirstOrDefault(s => s.Id == id);
if (scenario != null)
{
context.Scenarios.Remove(scenario);
context.SaveChanges();
}
}
Dispatcher.CurrentDispatcher.Invoke(new Action(() => ScenarioList.Delete(id)));
}
}
public class ScenarioPermissions : BusinessListBase<ScenarioPermissions, ScenarioPermission>
{
public static ScenarioPermissions NewPermissions()
{
return DataPortal.Create<ScenarioPermissions>();
}
public static ScenarioPermissions GetUsersByScenario(Scenario scenario)
{
return DataPortal.FetchChild<ScenarioPermissions>(scenario);
}
private void Child_Fetch(Scenario obj)
{
using (var ctx = DbContextManager<DatabaseContext>.GetManager())
{
var context = ctx.DbContext;
var scenario = context.Scenarios.Where(s => s.Id == obj.Id).FirstOrDefault();
if (scenario != null)
{
foreach (var item in scenario.Users)
{
this.Add(ScenarioPermission.NewPermission(scenario.Id, item.Id));
}
}
}
}
}
public class ScenarioPermission : BusinessBase<ScenarioPermission>
{
private static readonly PropertyInfo<Guid> m_ScenarioID = RegisterProperty<Guid>(p => p.ScenarioID);
public Guid ScenarioID
{
get { return GetProperty(m_ScenarioID); }
private set { SetProperty(m_ScenarioID, value); }
}
private static readonly PropertyInfo<int> m_UserID = RegisterProperty<int>(p => p.UserID);
public int UserID
{
get { return GetProperty(m_UserID); }
private set { SetProperty(m_UserID, value); }
}
public static ScenarioPermission NewPermission(Scenario scenario, Model.Users.User user)
{
return NewPermission(scenario.Id, user.Id);
}
public static ScenarioPermission NewPermission(Guid scenarioID, int userID)
{
var newObj = DataPortal.CreateChild<ScenarioPermission>();
newObj.ScenarioID = scenarioID;
newObj.UserID = userID;
return newObj;
}
private ScenarioPermission() { /* Used for Factory Methods */}
private void Child_Insert(Scenario scenario)
{
DataPortal_Insert();
}
private void Child_DeleteSelf(Scenario scenario)
{
DataPortal_DeleteSelf();
}
private void Child_DeleteSelf()
{
DataPortal_DeleteSelf();
}
protected override void DataPortal_Insert()
{
using (var ctx = DbContextManager<DatabaseContext>.GetManager())
{
var context = ctx.DbContext;
var scenario = context.Scenarios.FirstOrDefault(s => s.Id == ScenarioID);
var user = context.Users.FirstOrDefault(u => u.Id == UserID);
if (scenario != null && user != null)
{
scenario.Users.Add(user);
context.SaveChanges();
}
}
}
protected override void DataPortal_DeleteSelf()
{
using (var ctx = DbContextManager<DatabaseContext>.GetManager())
{
var context = ctx.DbContext;
var scenario = context.Scenarios.FirstOrDefault(s => s.Id == ScenarioID);
var user = context.Users.FirstOrDefault(u => u.Id == UserID);
if (scenario != null && user != null)
{
if (scenario.Users.Contains(user))
{
scenario.Users.Remove(user);
context.SaveChanges();
}
}
}
}
public override bool Equals(object obj)
{
// If parameter is null return false.
if (obj == null)
{
return false;
}
// If parameter cannot be cast to ScenarioPermission return false.
ScenarioPermission p = obj as ScenarioPermission;
if ((System.Object)p == null)
{
return false;
}
// Return true if the fields match:
return (this.ScenarioID == p.ScenarioID) && (this.UserID == p.UserID);
}
}