NPOCO. Is it possible to insert nested objects?

413 views Asked by At

I have a Dto class with two nested Dtos:

[TableName("ProductComponentVariant")]
    public class ProductComponentVariantDto
    {
        [Column("Id")]
        public int Id { get; set; }

        [Reference(ReferenceType.Foreign, ColumnName = "ProductId", ReferenceMemberName = "Id")]
        public ProductDto Product { get; set; }

        [Reference(ReferenceType.Foreign, ColumnName = "ComponentVariantId", ReferenceMemberName = "Id")]
        public ComponentVariantDto ComponentVariant { get; set; }
    }

The table in DB looks like:

Id                 int  (PK)
ProductId          int  (FK)
ComponentVariantId int  (FK)

Is it possible, when creating a ProductComponentVariant also set ProductId and ComponentVariantId. Because now I have an error "Invalid cast exception" The given value of type ProductDto from the data source cannot be converted to type int of the specified target column

1

There are 1 answers

0
jkmehmi On

I think it will be better if you pass the already converted value rather than converting in query.

Use any variable of type int and assign it to that particular property.