Most developers would consider many to many relationships a fairly standard database design.However, LINQ to SQL offers no native support for these types of relationships.PLINQO makes it very simple to work with many to many relationships.
Run Custom Tool Dbml How To Manually AddWe are going to walk you through how this was implemented in PLINQO by showing how to manually add support for many to many relationships. A user can have many roles and a role can contain many users. We will use a sample database containing the three tables that make up this relationship. To get the roles for a user, we must step through the UserRole list and get the associated Roles. Being forced to go through this process for each many to many relationship is tedious and frustrating, especially since these relationships are common. We should be able to see the roles for a user directly since that is the actual relationship we are concerned about. We need to add the DeleteOnNull attribute for each association in the UserRole entity. Run Custom Tool Dbml Update The DBMLSince the designer doesnt support managing this attribute, we will have to manually update the DBML to make this change. To update the DBML directly, right-click on the DBML file, select Open with and select the XML Editor. We need to add the DeleteOnNull attribute for each association and set it to true. Run Custom Tool Dbml Plus The AbilitySo, we will need a list of Roles plus the ability to handle adding and removing the roles from the relationship. We will add the following Roles property to the User object to do this. This abstracts away all of the plumbing associated with many to many relationships. Creating this property will make it possible to write the following line of code. We must be able to add and remove Roles and update the database accordingly. When a new instance EntitySet is instantiated, we can supply handlers for these actions. To handle our case, we have created the OnRolesAdd and OnRolesRemove event handlers. When a Role is added to the role list, we will also add a UserRole object to the UserRole list containing the appropriate User and Role objects that make up the relationship. When a Role is deleted, we will find the matching object in the UserRole list and remove it.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |