我不知道实体框架,但这是使用 NHibernate 在 RIA 中的工作原理......
我的型号是Users - UserRoleGrant - Role
。您必须在脑海中对模型进行翻译。
以下代码的重要部分是......确保您的模型上具有正确的关联名称,确保您在关联中设置了正确的属性名称,在设置 UserRoleAssociation 的 User 属性时设置 UserID 属性。如果您不设置此 ID,您将无法通过关联属性访问相关实体。
您可能也不需要 Composition 属性,但您可能会阅读此内容以找出... 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;
}
}
}
}