【问题标题】:Additional column in query generated by Entity Framework实体框架生成的查询中的附加列
【发布时间】:2014-03-20 14:34:18
【问题描述】:

我通过Oracle.ManagedDataAccess.Client 使用面向Oracle 11g 数据库的EF5 和.NET 4.5。我设置了一张小桌子来测试它的工作原理。 现在这是一个奇怪的事实,在搜索网络或本网站时均未显示任何结果。在每个查询中,我都有最后一列,例如 "Extent1"."Text_TextID" !!!这显然使 Oracle 抛出错误Invalid identifier,因为我没有具有此类名称的列,也没有数据库中的另一个对象。 无论我有多少表/列以及如何命名它们,都会发生这种情况(如果我有多个表,查询中都会有这个额外的列)。

有人知道为什么会这样吗??

示例代码如下:

//POCO class and mapping
[Table("LO_USERS")]
public class User 
{
    [Key]
    [Column("USER_ID")]
    public int UserID { get; set; }
}

//define the context

public class TestContext : DbContext
{
    public TestContext():base("OracleConn")
    {
    }

    public DbSet<User> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //replace the annoying dbo schema name. Note: even if I remove this, I still get the extra column in the query
        modelBuilder.Entity<User>().ToTable("LO_USERS", "TEST_SCHEMA");
    }

//create a new user
    using (var db = new TestContext())
        {
            var user = new User();
            db.Users.Add(user);
            //here I set a breakpoint
            db.SaveChanges();
        } 

VS2012 在断点处显示的查询:

SELECT 
1 AS "C1", 
CAST( "Extent1"."USER_ID" AS number(10,0)) AS "C2",
"Extent1"."Text_TextID" AS "Text_TextID"
FROM "TEST_SCHEMA"."LO_USERS" "Extent1"

编辑:

EF6 和 DotConnect 也是如此。

【问题讨论】:

    标签: oracle oracle11g entity-framework-5


    【解决方案1】:

    我找到了:问题是我将另一个类中的 User 类作为子对象引用,例如

    public class Text 
    {
        public virtual ICollection<User> Users { get; set; }
    

    没有在用户类中指定任何外键列,EF 试图自己设置一个。 一旦我删除了多余的列上方的行,select 语句就会消失。

    【讨论】:

      猜你喜欢
      • 2012-03-09
      • 1970-01-01
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      • 2017-09-25
      • 2017-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多