none
A problem with Add/Remove operation on EntitySet in DevArt LinqConnect RRS feed

  • General discussion

  • Hi, I asked the question on DevArt forum but haven't got any answer, may be somebody met the same trouble. The problem is that i have InvalidOperation exception when try to add and remove rows in the EntitySet . My Oracle database contains three tables (let's say User-UserRole-Role): 

    R(User)={ID(PK), Name} 
    R(Role) = {ID(PK), Name} 
    R(UserRole) = {UserID(PK/FK), RoleID(PK/FK), Description} 

    I have a WPF app, where i'm trying to give a user UI for editig UserRole data through the DataGrid control. For that purpose a have two commands: for add new row to UserRoles collection and remove one. In the very simplify way: 

    Код:
    public class ViewModel 

        User User {get;set;} 
        
        public AddUserRole(Role role) 
        { 
              if (user.UserRoles.Any(x=>x.Role == role)) return;  
              var newItem = new  UserRole(){Role = role}; 
              UserRoles.Add(newItem); 
        } 
        public RemoveUserRole(UserRole userRole) 
        { 
              if (!UserRoles.Contains(userRole)) return; 
              UserRoles.Remove(userRole); 
        } 

        IBindingList _userRoles; 
        public IBindingList UserRoles 
        { 
             get 
             { 
                  if (_userRoles  == null) _userRoles = user.UserRoles.GetNewBindingList(); 
                  return _userRoles;  
             }          
        } 


    Then i wrote a simple test: 

    Код:

    [TestMethod()] 
    void TestInsertRemove() 

         using (var db = new MyDataContext()) 
         { 
               ViewModel vm = new ViewModel(); 
               vm.User = db.Users.First(); 
               Role role = db.Roles.First(); 

               // Assume UserRoles table is empty - the user has no any roles 
               Assert.AreEqual(0, vm.User.UserRoles.Count); // OK 
               vm.AddUserRole(role); 
               Assert.AreEqual(1, vm.User.UserRoles.Count); // OK 
               var  lastAdded = vm.User.UserRoles.First(); 
               vm.RemoveUserRole(lastAdded); 
               Assert.AreEqual(0, vm.User.UserRoles.Count); // OK            

               // Try to add the role again 
               vm.AddUserRole(role);// Failed with System.InvalidOperationException: Can not insert entity with the same key if key is not database generated. 
         } 


    When i attampted to run this code on SQL Server + MS LINQ to SQL i got an exception only on SumbitChanges(). I've resolved the issue, added db.InsertOnSubmit(newItem) and db.DeleteOnSubmitChanges(userRole) in the methods: 

    Код:
    public AddUserRole(Role role) 
        { 
              if (user.UserRoles.Any(x=>x.Role == role)) return;  
              var newItem = new  UserRole(){Role = role}; 
              UserRoles.Add(newItem); 
              db.InsertOnSubmit(newItem);  // Assume db is accesible from ViewModel 
        } 
        public RemoveUserRole(UserRole userRole) 
        { 
              if (!UserRoles.Contains(userRole)) return; 
              db.DeleteOnSubmit(userRole); // Assume db is accesible from ViewModel 
              UserRoles.Remove(userRole); 
        } 

    In the LinqConnect this didn't help me. Help me, please!!!

     

    Saturday, January 22, 2011 11:49 AM

All replies

  • Hi Felix,

    I really want to help, but unfortunately I am not quite familiar with the DevArt provider.   I believe the best support channel is still the DevArt forum since they are the experts.  :)  

    Anyway, good luck!


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 24, 2011 8:29 AM
    Moderator