Can not have a property with the same name as a m2m relationship table.

Aug 28, 2012 at 8:31 AM
Edited Aug 28, 2012 at 8:34 AM

We are using v1 of m2m4ria (and will probably continue with using it for some time as we have our own ria t4 code generation which generates interfaces and well as design data) and have had some issues when we have properties with the same name as the "jointables".

The fix is quite simple, but since I did not find a tag or similar for v1 so we could submit a patch I post the problem, solution and patch here (v2 migth have the same problem) instead.

The Problem:

Assume we have the entities User and Role, with a assoiciation/join-table UserRole and where each user has a "UserRoleId" (with a navigation property named "UserRole").

m2m4ria will then generate the following method for the entity User, which can not compile since "UserRole" to the left of AttachRoleToUser will refer to the property and not the class:

private void AddUserRole(Role role)
{             
var newJoinType = new UserRole();
UserRole.AttachRoleToUser(newJoinType, this, role);
}

 The Solution:

 The solution is to specify the full namespace for the M2M link-table as in the code below

private void AddUserRole(Role role) 
{             
var newJoinType = new UserRole();             
Fully.Qualified.Namespace.UserRole.AttachRoleToUser(newJoinType, this, role);
}

Patch for m2m4ria v1:
Change line 351 in M2M4RiaDomainContext.ttinclude
from
  

<#= association.Name #>.Attach<#= association.OtherEntityToLink.EntityNavProp #>To<#= association.ThisEntityToLink.EntityNavProp #>(newJoinType, this, <#= LowerInitial(association.OtherEntityToLink.EntityNavProp)#>);
to
  
<#= EntityModelNamespace #>.<#= association.Name #>.Attach<#= association.OtherEntityToLink.EntityNavProp #>To<#= association.ThisEntityToLink.EntityNavProp #>(newJoinType, this, <#= LowerInitial(association.OtherEntityToLink.EntityNavProp)#>);

 

Aug 29, 2012 at 9:07 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.