2
votes

ok, I know this is asked a million times, but I still can't seem to find a working solution. Hopefully I'm just missing something. Using entity with silverlight, through RIA I have a many to many table structure in my DB.

[System] - [SystemUsers] - [Users] 

The bridge table just has the two IDs so naturaly entity doens't include it in the model.

I know RIA doesn't like many to many relationships, so I've added Association attribute tags to the collection, and can now at least see the collection property on the client side.

[Association("DMSSystem_Users", "DMSSystemId", "UserId")]
[Include]
[Composition]
public EntityCollection<Users> Users { get; set; }

In my domain service I've tried including the users:

public IQueryable<DMSSystem> GetSystem()
{
    return this.ObjectContext.DMSSystem.Include("Users");
}

I never get the users on the client side Is there something else I am missing to get the users to be inculded and sent to the client?

3

3 Answers

1
votes

I don't know Entity Framework but here's how this works in RIA with NHibernate...

My model is Users - UserRoleGrant - Role. You'll have to do the translation to your model in your head.

The important parts of the following code are... ensure you have the correct association names on your model, ensure you have the correct property names set in your associations, set the UserID property on the UserRoleAssociation when you set it's User property. If you don't set this ID you wont have access to the related entity via the assocation property.

You probably don't need the Composition attribute either but you might so read this to find out... http://ria.feedables.com/story/4583193/Composition-Support-in-RIA-Services

public class User 
{
    ...snip...
    [Include]
    [Association("UserToRoleAssociation", "Id", "UserId", IsForeignKey = false)]
    public virtual IList<UserRoleAssociation> RoleGrants
    { 
        get
        {
            return this.roleGrants;
        }
    }
}

public class UserRoleAssociation 
{
    /// <summary>
    /// Backing field for User
    /// </summary>
    private User user;

    /// <summary>
    /// Gets or sets the id.
    /// </summary>
    /// <value>The relationships id.</value>
    [Key]
    public virtual long Id { get; set; }

    /// <summary>
    /// Gets or sets the user id.
    /// </summary>
    /// <value>The assigned users id.</value>
    public virtual long UserId { get; set; }

    /// <summary>
    /// Gets or sets the user.
    /// [Association("UserRoleGrants", "UserId", "Id", IsForeignKey = false)]
    /// </summary>
    /// <value>The user who has been granted this operation.</value>
    [Include]
    [Association("UserToRoleAssociation", "UserId", "Id", IsForeignKey = true)]
    public virtual User User
    {
        get
        {
            return this.user;
        }

        set
        {
            this.user = value;

            if (value != null)
            {
                this.UserId = value.Id;
            }
        }
    }
}
1
votes

RIA with EF has a dislike for M:M relationships. What you need to do is to help RIA see the M:M as two 1:M relationships.

On your link table add an extra column (I add something like IgnoreThisField) and make it a bit type.

When EF sees this table it will now interpret it differently allowing you to handle the M:M relationship.

0
votes

Just ran across this: http://m2m4ria.codeplex.com/

It requires some setup, but exposes a view to the client which solves the whole many-to-many issue, without modifying your database or entity model. I found it very useful.