最近用Entity Framework 开发的时候,发现一个问题,在默认情况下,EF不能对一个没有主键的表进行更新、插入和删除的动作。

那么,应该怎么处理没有主键的表呢?

我们打开这个表的edmx文件,可以看到以下的xml片段

<EntitySet Name="REP" EntityType="Model.Store.REP" store:Type="Tables" store:Schema="FOREST" store:Name="REP">
<DefiningQuery>SELECT
"REP"."ZONE" AS "ZONE",
"REP"."NO" AS "NO",
"REP"."HELP_NO" AS "HELP_NO",
"REP"."NO_TYPE" AS "NO_TYPE",
"REP"."ID_NO" AS "ID_NO",
"REP"."NAME" AS "NAME",
"REP"."TEL" AS "TEL",
"REP"."MAIL_NO" AS "MAIL_NO",
"REP"."ADDRESS1" AS "ADDRESS1",
"REP"."ADDRESS2" AS "ADDRESS2",
"REP"."JOINDATE" AS "JOINDATE",
"REP"."ELITE_D" AS "ELITE_D",
"REP"."PD1" AS "PD1",
"REP"."CUR_CPS" AS "CUR_CPS",
"REP"."PREV_CPS1" AS "PREV_CPS1",
"REP"."YEAR_CPS" AS "YEAR_CPS",
"REP"."IA_CPS" AS "IA_CPS",
"REP"."PREV_BAL" AS "PREV_BAL",
"REP"."CUR_BAL" AS "CUR_BAL",
"REP"."ELITE" AS "ELITE",
"REP"."CUR_CAMP" AS "CUR_CAMP",
"REP"."SALES" AS "SALES",
"REP"."RETURN" AS "RETURN",
"REP"."NO_ACT" AS "NO_ACT",
"REP"."PD_NO" AS "PD_NO",
"REP"."CUR_ORDER" AS "CUR_ORDER",
"REP"."BIRTHDAY" AS "BIRTHDAY",
"REP"."RNAM" AS "RNAM",
"REP"."POST_YN" AS "POST_YN",
"REP"."CARD_NO" AS "CARD_NO",
"REP"."CARD_YYYYMM" AS "CARD_YYYYMM",
"REP"."TEL2" AS "TEL2",
"REP"."MOBILE" AS "MOBILE",
"REP"."EMAIL" AS "EMAIL",
"REP"."PREV_CPS2" AS "PREV_CPS2",
"REP"."SEMAIL" AS "SEMAIL",
"REP"."PASSWORD" AS "PASSWORD",
"REP"."NEXT_CPS" AS "NEXT_CPS",
"REP"."TRAN_D" AS "TRAN_D",
"REP"."PASS_CNT" AS "PASS_CNT",
"REP"."ORDER_CT" AS "ORDER_CT",
"REP"."ORDER_AT" AS "ORDER_AT",
"REP"."SNAME" AS "SNAME",
"REP"."NOTES1" AS "NOTES1",
"REP"."NOTES2" AS "NOTES2",
"REP"."PPWD" AS "PPWD",
"REP"."SALES6" AS "SALES6",
"REP"."NO5" AS "NO5",
"REP"."SALES4" AS "SALES4",
"REP"."DELIVER" AS "DELIVER",
"REP"."ISWHWB" AS "ISWHWB",
"REP"."LOS" AS "LOS",
"REP"."CREDITLINE" AS "CREDITLINE",
"REP"."OCCUPATION" AS "OCCUPATION",
"REP"."ENTRYLOCATION" AS "ENTRYLOCATION",
"REP"."SOURCE" AS "SOURCE",
"REP"."SURVEYTAG" AS "SURVEYTAG",
"REP"."HMDATE" AS "HMDATE",
"REP"."ISRCVSMS" AS "ISRCVSMS",
"REP"."PAYMETHOD" AS "PAYMETHOD",
"REP"."ISPBAMCOURSE" AS "ISPBAMCOURSE",
"REP"."ISBSDCOURSE" AS "ISBSDCOURSE",
"REP"."BSADATE" AS "BSADATE",
"REP"."CUR_NETSALES" AS "CUR_NETSALES",
"REP"."WEBPCDATE" AS "WEBPCDATE",
"REP"."WEBPC" AS "WEBPC",
"REP"."ISMCNOTICE" AS "ISMCNOTICE",
"REP"."OLDNO" AS "OLDNO",
"REP"."EMAILACCOUNT" AS "EMAILACCOUNT",
"REP"."MCDATE" AS "MCDATE",
"REP"."PASS_CRTDATE" AS "PASS_CRTDATE",
"REP"."STARXNO" AS "STARXNO",
"REP"."ZONEID" AS "ZONEID",
"REP"."ORDER_INFO" AS "ORDER_INFO",
"REP"."TRAN_D_EC_DATE" AS "TRAN_D_EC_DATE",
"REP"."REALTITLEID" AS "REALTITLEID",
"REP"."PAIDTITLEID" AS "PAIDTITLEID",
"REP"."PUBLICTITLEID" AS "PUBLICTITLEID",
"REP"."ISASLF" AS "ISASLF",
"REP"."ASLFROSTARTCAMP" AS "ASLFROSTARTCAMP",
"REP"."ASLFROENDCAMP" AS "ASLFROENDCAMP",
"REP"."ISVIRTUALID" AS "ISVIRTUALID",
"REP"."ASLFSPONSORID" AS "ASLFSPONSORID",
"REP"."MPCACTCPS" AS "MPCACTCPS",
"REP"."MPCORDVAL" AS "MPCORDVAL",
"REP"."ASLFCDBENDCAMP" AS "ASLFCDBENDCAMP",
"REP"."ASLFSTARTCAMP" AS "ASLFSTARTCAMP",
"REP"."ISDRCERT" AS "ISDRCERT",
"REP"."DRCERTSTARTCAMP" AS "DRCERTSTARTCAMP",
"REP"."DRCERTENDCAMP" AS "DRCERTENDCAMP",
"REP"."DRFOUSECAMP" AS "DRFOUSECAMP",
"REP"."MAIL_NO1" AS "MAIL_NO1",
"REP"."ADDRESS12" AS "ADDRESS12",
"REP"."ADDRESS22" AS "ADDRESS22",
"REP"."RECOMMENDERID" AS "RECOMMENDERID",
"REP"."RECOMMENDERNAME" AS "RECOMMENDERNAME",
"REP"."RECOMMENDERNATIONALID" AS "RECOMMENDERNATIONALID",
"REP"."COAPPLICANTNAME" AS "COAPPLICANTNAME",
"REP"."COMPANYNAME" AS "COMPANYNAME",
"REP"."ISEINVOICE" AS "ISEINVOICE",
"REP"."ISDONATE_INVOICE" AS "ISDONATE_INVOICE",
"REP"."DRM_CREATEDDATE" AS "DRM_CREATEDDATE"
FROM "FOREST"."REP" "REP"</DefiningQuery>
</EntitySet>

我再加入一个有主键的表进行对比,可以看到有主键的表的定义如下。

<EntitySet Name="City" EntityType="Model.Store.City" store:Type="Tables" Schema="FOREST" />

我们把没有主键的<EntitySet>照着上面这个节点进行更改:删除<DefiningQuery>节点,将store:Schema=”FOREST”更改为Schema=”FOREST”。这样我们就可以对之前没有设置主键的表进行更新、删除以及插入操作了。

无主键的表SSDL定义其实更像是视图,我有一点不明的是store:这个命名空间的作用是什么,为什么只是删除<DefiningQuery>不行,还需要将Schema属性的store命名空间删除才可以。以上都是我还不明白的地方,只是作为一个解决方案,它确实简单可行。

04-14 12:38