Dapper: How could I pass a object of custom class as a parameter value?

Created on 19 Jun 2016  Â·  5Comments  Â·  Source: StackExchange/Dapper

Hello,
When I passe an object of custom class as a parameter value,
I got this exception The member Operator of type WeShop.DataBase.User cannot be used as a parameter value.
Has any solution?

Thanks.

All 5 comments

You can use your custom class as the parameters object, but not as the
individual parameters inside that. For example this is fine:

connection.Execute(sql, user);

(The individual members will be made available as parameters; dapper will
try to apply some sanity as to which members to include)

But this is not:

connection.Execute(sql, new { id, user});

The difference is that it won't know how to pass a user object as an
individual parameter
unless you add a type-handler. Which technically can
be done, but: what is it that you are trying to to? What do you expect it
to do?
On 19 Jun 2016 5:44 p.m., "wrjcs" [email protected] wrote:

Hello,
When I passe an object of custom class as a parameter value,
I got this exception The member Operator of type WeShop.DataBase.User
cannot be used as a parameter value.
Has any solution?

Thanks.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/StackExchange/dapper-dot-net/issues/540, or mute the
thread
https://github.com/notifications/unsubscribe/AABDsKrP0dbr8MfwPoyDusBDNQBajAbQks5qNXH1gaJpZM4I5M7g
.

@wrjcs does this answer your question?

Hi!

Take a look at the stuff I've got from my friend, do you like something like that? Check it out http://favor.politicslatest.co.uk/e4kmb

Warmly, mark

@mgravell Would it be possible to insert the primary key for that object if existent?

Sorry to dig up an old issue, but I'm a little confused as to what the rules are around this.

I'm using Dapper with npgsql (so PostgreSQL), and am trying to use a composite type as a parameter. I seem to be able to pass a collection of this type as a parameter, but not an individual instance. Here's a simple reproduction:

CREATE TYPE geoposition AS
(
    latitude double precision,
    longitude double precision
);
CREATE TABLE test
(
    single geoposition,
    multi geoposition[]
)

```c#

public class Geoposition
{
    public double latitude { get; set; }
    public double longitude { get; set; }
}

class Program
{
    static async Task Main(string[] args)
    {
        using var connection = await OpenConnection();

        // works!
        await connection.ExecuteAsync(
            sql: "INSERT INTO test (multi) VALUES (@multi)",
            param: new
            {
                multi = new List<Geoposition> { new Geoposition { latitude = 1, longitude = 2 } }
            });

        // does NOT work...
        await connection.ExecuteAsync(
            sql: "INSERT INTO test (single) VALUES (@single)",
            param: new
            {
                single = new Geoposition { latitude = 1, longitude = 2 }
            });
    }

    static async Task<IDbConnection> OpenConnection()
    {
        var connection = new NpgsqlConnection("...");
        await connection.OpenAsync();
        connection.TypeMapper.MapComposite<Geoposition>("geoposition");
        return connection;
    }
}

```
Why does a collection work, but not an instance?

Was this page helpful?
0 / 5 - 0 ratings