我有自定义实体(不是来自实体模型),它有一个属性,返回EF实体的集合(来自实体模型):
[DataContract]
public class MyEntity
{
[DataMember]
public List<Role> Roles { get; set; }
}
'Role'和'RolePermission'实体由EF4从DB生成。 RolePermission具有FOREGIN_KEY到角色,并且EF4在Role和RolePermission之间生成了关联:
Role.RolePermissions --navigate属性 RolePermission.Role --navigate property
另外,我有DomainService:
[EnableClientAccess()]
public class MyEntityService : DomainService
{
public List<MyEntity> GetMyEntities()
{
...
myEntityInstance.Roles = <GetRoles>
...
return <collection of MyEntities with Roles>
}
}
当我尝试编译时,我收到错误:
Entity 'UserManager.Web.RolePermission' has a property 'RoleReference' with an unsupported type
当我将[Include]属性放入MyEntity.Roles属性时,我得到相同的错误并出现此错误:
Property 'Roles' of complex type 'MyEntity' is invalid. Complex types cannot have include members.
当我手动(从实体模型)删除RolePermission到Role(RolePermission.Role导航属性)的引用时,我在编译时只得到这个错误:
The Entity 'Role' in DomainService 'RolesService' does not have a key defined. Entity types exposed by DomainService operations must have at least one public property marked with the KeyAttribute.
我该如何解决这种情况?如何从MyEntityService返回带有填充Roles属性的自定义对象(MyEntity)?
添加[key] attr到Role.Metadata,并编译成功。但是客户端上没有MyEntity.Roles属性。
答案 0 :(得分:0)
RIA服务要求在客户端到服务器之间来回传递的所有对象都有唯一的密钥,以便它知道您正在修改哪个特定对象。
如果您必须拥有自己的对象作为EF对象的包装器,只需添加一个标有[key]的id成员并自己维护该值。
[DataContract]
public class MyEntity
{
[key]
public int Id { get; set; }
[DataMember]
public List<Role> Roles { get; set; }
}
如果您需要这样做,设计似乎有问题。您的应用程序中的一组角色的父级是什么?为什么不只是查询角色?