实体框架错误,插入命令生成

时间:2011-03-09 16:05:45

标签: c# sql entity-framework postgresql

我使用Entity Framework处理PostgreSql DB: 当我向DB中添加新项时,它会生成奇怪的代码:

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))

所以添加

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

而不是表名“public”。“person_contact”

如何解决此实体框架错误???

UPD:当我尝试删除“person_contact”条目时出现同样的问题。在delete语句而不是表名中 - 选择查询。

2 个答案:

答案 0 :(得分:1)

有几种方法可以尝试解决此问题:

这些按照我尝试的顺序列出。

答案 1 :(得分:0)

很可能你忘了在这张桌子上创建主键。

我遇到了同样的问题,我的案例中的解决方案非常简单。问题是我有一个名为“id”的列,但我忘记将其作为主键。我把它设置为主键的那一刻一切都还可以。

这很奇怪,因为EF,normaly不会导入没有主键的表,但是当你有一个名为“id”的列时,它会认为它是一个主键。

我桌子的结构是:

*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)*

由NpgSql INSERT语句生成:

*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"*

解决方案只是创建一个主键:

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