Search code examples
sqlasp.net-coredapper

Net core dapper insert model inside model error: the member cannot be used as a parameter value


Below, I have my main model MyModel.

Options is a property of type Options.

In PostGres Options is a jsonb column.

public class MyModel
{
    public string Id { get; set; }
    public Options Options { get; set; } = new Options();
}

Options model:

public class Options
{
    public string Test { get; set; }
}

Using Dapper I insert like this:

public async Task Create(MyModel model)
{
string sql = "insert into MyModel (id, options) values (@id, @options)"
....

I get this error:

"The member Options of type MyApp.Models.Api.Options cannot be used as a parameter value"

How does Dapper know about the options model I am sending in? Should I be serialising Options before sending to Dapper?

I cannot find many examples of this kind of thing where there's a model inside a model.


Solution

  • Using CAST(@options as json) in my SQL sorted the error.