Reputation: 647
I'm trying to pass C# object model as postgresql type to function by dapper but I get this error:
System.NotSupportedException: The CLR type System.Data.DataTable isn't natively supported by Npgsql or your PostgreSQL. To use it with a PostgreSQL composite you need to specify DataTypeName or to map it, please refer to the documentation.
My postgresql type is image
with url property.
This is my function in postgresql:
CREATE OR REPLACE FUNCTION public.createapp(applicationid text, images image)
RETURNS void
LANGUAGE 'plpgsql'
AS $BODY$
BEGIN
INSERT INTO app
(
id,
applicationid,
images
)
VALUES
(
uuid_generate_v4(),
applicationid,
images
);
END
$BODY$;
My image model is:
public class Image
{
public string Url { get; set; }
}
And this is my C# Dapper repository code:
var parameters = new DynamicParameters();
parameters.Add("applicationid", application.ApplicationId);
parameters.Add("images", application.Images);
var result = await GetDb().QueryFirstOrDefaultAsync("createapp", parameters, commandType: CommandType.StoredProcedure);
How can I pass this parameter to my function?
Upvotes: 2
Views: 2404
Reputation: 647
I solved this by add MapComposite in startup.cs:
NpgsqlConnection.GlobalTypeMapper.MapComposite<Model.Entities.Image>("image");
That "image"
is my postgresql type.
Upvotes: 3
Reputation: 42
you need to register your custome type in startup like this :
SqlMapper.AddTypeHandler(new PgTypeHandler<SomeType>());
and then define your type in sql script parameter like this: @images::sometype
internal class PgTypeHandler<T> : SqlMapper.TypeHandler<T>
{
public override T Parse(object value)
{
return (T)value;
}
public override void SetValue(IDbDataParameter parameter, T value)
{
parameter.Value = value;
}
}
more document: https://www.npgsql.org/doc/
Upvotes: 1