简体   繁体   中英

Entity Framework bug, insert command generation

I am working with PostgreSql DB using Entity Framework: When I add new item into DB it generates strange code:

INSERT INTO (SELECT "person_contact"."person_id" AS "person_id",
             "person_contact"."contact_id" AS "contact_id" 
             FROM "public"."person_contact" AS "person_contact")
       ("person_id","contact_id") 
       VALUES (cast(141792 as int8),cast(289406040 as int8))

So it add

SELECT "person_contact"."person_id" AS "person_id",
"person_contact"."contact_id" AS "contact_id" 
FROM "public"."person_contact" AS "person_contact"

instead of table name "public"."person_contact"

How to resolve this Entity Framework bug ???

UPD: Same issue when I try to delete "person_contact" entry. In delete statement instead of table name - select query.

There are several ways to try and fix this:

These are listed in the order that I would try them.

Most likely you forgot to create primary key on this table.

I've had the same problem and the solution in my case was very simple. The problem was that I had a column named "id", but I forgot to make it Primary Key. The moment I set it as Primary Key everything was OK.

It is very strange, because EF, normaly won't import table without primary key, but when you have column named "id" it assumes that it is a primary key.

The structure of my table was:

*DROP TABLE IF EXISTS "public"."fact_season_tickets";
CREATE TABLE "public"."fact_season_tickets" (
"id" int8 DEFAULT nextval('fact_season_tickets_id_seq'::regclass) NOT NULL,
"season_ticket_id" int8 NOT NULL,
"date_key" int4 NOT NULL,
"station_id" int4 NOT NULL,
"amount" numeric(18,2) DEFAULT 0 NOT NULL,
"status" int4 NOT NULL
)
WITH (OIDS=FALSE)*

The generated by NpgSql INSERT statement was:

*INSERT INTO (SELECT "fact_season_tickets"."id",
    "fact_season_tickets"."season_ticket_id",+
    "fact_season_tickets"."date_key", 
    "fact_season_tickets"."station_id", 
    "fact_season_tickets"."amount", 
    "fact_season_tickets"."status" 
FROM "public"."fact_season_tickets" AS "fact_season_tickets") 
("season_ticket_id","date_key","station_id","amount","status") 
VALUES (510::int8,20150630,2,18.00::numeric,1) 
RETURNING "id"*

The solution was just creating a primary key:

*ALTER TABLE "public"."fact_season_tickets" ADD PRIMARY KEY ("id");*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM