While writing Unit Tests for my membership provider code, I tried adding a permissionI had defined on one test user to a different test user. I was completely freakedout, since user1 suddenly no longer had that permission.
What kind of witchery is this? Does Collection<T>.Add() remove items from thesource collection?
No. It doesn’t. I tried it all with simple collections. List<string>.Add() doesn’thave this effect. So what’s going on?
The problem has to be in my persistence layer. My User and Permissions objects areprovided by LLBLGen. So it’s gotta be something in there. (A light dawns). I lookback at my table definitions. Sure enough, a permission as a m:1 relationship withit’s user. A permission entity can’t belong to more than one user. And LLBLGen issmart enough that when I take the object in the graph and add it to a different user,it not only updates the relationship(FK), but removes it from the original object.
But how do I get the result I wanted? A simple duplication of an existing permission?I adapted the deepcopy method I found over on Stack Overflow into a general extension method andused it to dupe the permission as I added it. Clean copy in the object graph=>nochanges to the relationship(FK)=>success.