How to call a bank function from code

2

I would like some help,

I have the following function that is in the database postgres

CREATE OR REPLACE FUNCTION public.get_id(campo character varying)
  RETURNS integer AS
$BODY$
DECLARE
  _id integer;
BEGIN
  select into _id vlr_corrente from system_generators where nome = $1;
  if _id is null 
  then
   insert into system_generators(nome, vlr_corrente) Values(campo, 1);
   select into _id vlr_corrente from system_generators where nome = $1;
   update system_generators set vlr_corrente = vlr_corrente + 1 where nome = $1;
  else
   select into _id vlr_corrente from system_generators where nome = $1;
   update system_generators set vlr_corrente = vlr_corrente + 1 where nome = $1;
  end if;

  return _id;

END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION public.get_id(character varying)
  OWNER TO postgres;

This function generates the primary keys for me, to generate the primary keys, I just have to call it: select public.get_id('VENDA') , it returns me a unique number.

So that's fine, but as I call the function that is in the database, via the line of code using EntityFramework/C#/MVC and returning the number.

Or if possible I can generate the primary keys from the same code, without having to call the function, I already tried to use:

[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]

        public int id { get; set; }

asked by anonymous 29.08.2017 / 17:17

1 answer

1

The same way you do the SELECT.

var query = "SELECT functionName() as Result FROM Dual";
var result = con.ExecuteScalar(query);
    
30.08.2017 / 17:40