EntityFramework 4.1 DbContext选择添加带有附加字符的CAST列

时间:2013-09-18 18:03:25

标签: entity-framework entity-framework-4.1 dbcontext

我正在从dbcontext执行标准的GetAll():

 DbContext.Set<T>()

但是我从oracle那里得到了一个奇怪的消息:

  

{&#34; ORA-00904:\&#34; Extent1 \&#34;。\&#34; Sub_Object_ID \&#34;:无效的标识符&#34;}

如果我查看生成的sql(通过查看查询变量),我看到最后添加了一些变量作为CASTS

"Extent1"."SomeEntity_ID",                            <-- correct
"Extent1"."SomeEnttiy2_ID",                           <-- correct
"Extent1"."Sub_Object",                               <-- correct

CAST( "Extent1"."SomeEntity_ID1" AS number(10,0)) AS "C3",  <-- "1" appended
CAST( "Extent1"."SomeEnttiy2_ID1" AS number(10,0)) AS "C4", <-- "1" appended
CAST( "Extent1"."Sub_Object_ID" AS number(10,0)) AS "C5",   <-- "_ID" appended
...
FROM "dbo"."MyEntity" "Extent1"

所有属性都在select的主要部分中正确识别。但是在CAST部分,属性名称附加了数字和_ID ..这导致select失败..

查看我的实体,我有一次指定的属性..以这种格式:

    public Nullable<decimal> SomeEntity_ID { get; set; }
与演员阵容有什么关系?

1 个答案:

答案 0 :(得分:0)

这只是配置外键的问题。我仍然不明白这种默认行为的意图(为附加“1”的每个外来添加一组选择列)。

但声明外键修复了它。

通过流畅的API:

modelBuilder.Entity<FirmPerson>()
    .HasRequired(f => f.Firm)
    .WithMany(p => p.FirmPerson)
    .HasForeignKey(f => f.FirmID);

或通过属性:

    public int FirmID { get; set; }

    [ForeignKey("FirmID")]
    public virtual Firm Foo { get; set; }