插入视图不返回主键

时间:2016-06-29 12:33:46

标签: postgresql entity-framework-6 npgsql

我已将POCO映射到视图。当EF执行插入时,它会生成以下命令...

INSERT INTO "public"."vw_place"("Name","Description","Point") VALUES ($1,$2,$3) RETURNING "PlaceId"

但是代码抛出了以下错误......

  

{“为不可为空的成员返回了一个空存储生成的值   “PlaceId”类型为“DataAccess.Place”。“}

这是执行插入的触发器函数...

CREATE OR REPLACE FUNCTION vw_place_dml()
  RETURNS trigger AS
$BODY$
                   BEGIN
                      IF TG_OP = 'INSERT' THEN

                        INSERT INTO tbl_place
                        (
                        "Name",
                        "Description",
                        "Point"
                    )
                    VALUES
                    (
                        NEW."Name",
                        NEW."Description",
                        ST_GeomFromWKB(NEW."Point", 4326)
                    );

                            RETURN NEW;

                            ELSIF TG_OP = 'UPDATE' THEN

                          UPDATE tbl_place SET
                        "PlaceId" = NEW."PlaceId",
                        "Name" = NEW."Name",
                        "Description" = NEW."Description",
                        "Point" = ST_GeomFromWKB(NEW."Point", 4326)
                    WHERE
                        "PlaceId" = OLD."PlaceId";

                            RETURN NEW;

                            END IF;
                            RETURN NEW;
                            END;
                $BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION vw_place_dml()
  OWNER TO postgres;

如果我在pgAdmin中运行它会返回任何内容,这就是问题所在,但是成功插入了行并生成了PlaceId。

INSERT INTO "public"."vw_place"("Name","Description","Point") VALUES
('ergw','regr','\x0101000000000000009846b2bfe4e068d7ff33a73f')

RETURNING "PlaceId"

1 个答案:

答案 0 :(得分:0)

在触发器功能中插入后RETURN NEW;之前,我添加了......

NEW."PlaceId" = currval('"tbl_place_PlaceId_seq"');