RIA services are not refundable.

I have a WCF RIA services application and a model with type UserRole that contains a collection of UserPermission objects. I use .Include ("UserPermission") in the domain service, and when I debug it, I checked that it definitely contains UserPermission types before returning.

When I debug the Silverlight 3 client, it returns UserRoles, but the UserPermission properties are empty. These are the same UserRoles that indicate that UserPermissions are in the service.

Since everything looks right for the service and the client, I focus on the metadata class, but still can't find anything bad.

[MetadataTypeAttribute(typeof(UserRole.UserRoleMetadata))]
public partial class UserRole
{
    internal sealed class UserRoleMetadata
    {
        public int RoleID;
        public string Name;

        [Include]
        [Association("UserPermissions", "RoleID", "PermissionID")]
        public EntityCollection<UserPermission> UserPermissions;
    }
}

Here's the domain service method:

public IEnumerable<UserRole> GetUserRoles()
{
    IEnumerable<UserRole> roles = this.ObjectContext.UserRole.Include("UserPermissions");
    return roles; // In debug, roles.First().UserPermissions.Count = 2 here
                  // For now, there is only one single role in the ObjectContext and it has
                  // two UserPermissions
}

Here's the Silverlight client method:

context.Load(context.GetUserRolesQuery(), loadOp =>
{
    IEnumerable<UserRole> roles = loadOp.Entities;
    // This should show 2, but shows 0:
    MessageBox.Show("Permissions loaded: " + roles.First().UserPermissions.Count.ToString());
}

- -, ? , .

+3
1

, ! , , Fiddler , . , - , " " EF , , .

, :

1) (, " " ), . EF- .

2) , , , , , . , - , , , , .

3) , . VS ( "meta" ) . .

4) / , AssociationAttributes :

[MetadataTypeAttribute(typeof(UserPermissionMembers.UserPermissionMembersMetadata))]
public partial class UserPermissionMembers
{
    internal sealed class UserPermissionMembersMetadata
    {
        private UserPermissionMembersMetadata()
        {}

        public int ID;
        public UserRole UserRole;

        [Include]
        [Association("UserPermission", "fkPermissionID", "PermissionID", IsForeignKey = true)]
        public UserPermission UserPermission;
    }
}

5) :

public IEnumerable<UserRole> GetUserRoles()
{
    IEnumerable<UserRole> roles = this.ObjectContext.UserRole.Include("UserPermissionMembers.UserPermission");
    return roles;
}

6) .

context.Load(context.GetUserRolesQuery(), loadOp =>
{
    IEnumerable<UserRole> roles = loadOp.Entities;
    MessageBox.Show("Permissions loaded: " + roles.First().UserPermissionMembers.Count.ToString());
}

. , , AssociationAttributes, . , .

-, . EF v4, , .

+7

Source: https://habr.com/ru/post/1740809/


All Articles