5

I have a class like this:

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

  public AuthResource Resource { get; set; }

  public int ActionId { get; set; }
}

where AuthResource is:

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

  public int ResourceType { get; private set; }
}

The table where the data is stored has the following fields:

  • Id
  • ResourceId
  • ResourceType
  • ActionId

I can read the AuthEntity well (including Resource property) but I don't know how do I insert AuthEntity records. NPoco says that there is no Resource field. How can I map the nested object fields to the table fields?

Thanks

dsancho
  • 51
  • 2

2 Answers2

2

The NPoco documentation says you can do this with Mapping To Nested Objects .

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

Also you have to change your AuthResource to

public class AuthResource
{
     public long ResourceId { get; set; }
     public int ResourceType { get; set; }
}
Ajay Peter
  • 153
  • 4
  • 1
    The read operation isn't the problem, it works well. What I want to do is insert the AuthEntity object in the database and automatically map AuthResource properties to the ones in the table. – dsancho Sep 04 '14 at 05:25
1

Your table structure indicates that ResourceId and ResourceType are members of AuthEntity, and are not a separate class. If you wanted to have AuthResource as a separate class you could make AuthEntity inherit from AuthResource e.g.

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; }
}

in this way AuthEntity would contain the ResourceId and ResourceType values that you want it to have and AuthResource can be re-used for other classes that implement the same structure, which, it seems to me, is what you’re looking for.

Greg
  • 515
  • 1
  • 8
  • 19