5

我有这样的课:

public class AuthEntity 
{
  public int Id { get; set; }

  public AuthResource Resource { get; set; }

  public int ActionId { get; set; }
}

其中 AuthResource 是:

public class AuthResource 
{
  public long ResourceId { get; private set; }

  public int ResourceType { get; private set; }
}

存储数据的表具有以下字段:

  • ID
  • 资源 ID
  • 资源类型
  • 操作 ID

我可以很好地阅读 AuthEntity(包括 Resource 属性),但我不知道如何插入 AuthEntity 记录。NPoco 说没有 Resource 字段。如何将嵌套对象字段映射到表字段?

谢谢

4

2 回答 2

2

NPoco 文档说您可以使用Mapping To Nested Objects来做到这一点。

db.Fetch<AuthEntity, AuthResource>("select query to db")

您还必须将 AuthResource 更改为

public class AuthResource
{
     public long ResourceId { get; set; }
     public int ResourceType { get; set; }
}
于 2014-08-19T04:34:23.560 回答
1

您的表结构表明 ResourceId 和 ResourceType 是 AuthEntity 的成员,而不是单独的类。如果您想将 AuthResource 作为一个单独的类,您可以使 AuthEntity 从 AuthResource 继承,例如

public class AuthResource
{
     public long ResourceId { get; private set; }
     public int ResourceType { get; private set; }
}

public class AuthEntity : AuthResource
{
    public int Id { get; set; }   
    public int ActionId { get; set; }
}

这样 AuthEntity 将包含您希望它具有的 ResourceId 和 ResourceType 值,并且 AuthResource 可以重新用于实现相同结构的其他类,在我看来,这就是您正在寻找的。

于 2014-08-19T03:55:19.047 回答