开发者

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开发者_JS百科 name - select query.


There are several ways to try and fix this:

  • Firstly, it could be that your model has become corrupt. You could try deleting the model and recreating it. Also see my answer to this question: SQL Server foreign keys messing with entity framework model

  • Secondly, you say that it only happens with this table. Is there anything special about this table.

  • Thirdly, you could try a different .net connector for ProgressSQL, see: http://www.devart.com/dotconnect/entityframework.html

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");*
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜