Bonjour à tous,
Je suis nouveau avec CSLA.Net et j'ai quelques soucis pour supprimer un objet enfant d'une liste CSLA.
Pour mon problème, j'ai 3 classes :
-Scenario (BusinessBase<Scenario>)
-ScenarioPermissions (BusinessListBase<ScenarioPermissions, ScenarioPermission>)
-ScenarioPermission (BusinessBase<ScenarioPermission>)
La classe Scenario contiens un attribut de type ScenarioPermissions.
Si je fait un Scenario.Permissions.Add(obj) et Scenario.Save(), tout fonctionne correctement, et la permission est bien ajoutée en DB.
Mais si je veux faire Scenario.Permissions.Remove(obj) et Scenario.Save(), il passe le save sans problème mais ne supprimer pas mon objet ScenarioPermission de la db.
D'avance merci pour l'aide,
Franck.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 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))); } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 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)); } } } } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 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); } }
Partager