EF Core(Oracle)不明确的列

mi7gmzs6  于 2022-11-03  发布在  Oracle
关注(0)|答案(1)|浏览(214)

我有下面的类(继承):

public class Animal
{
   public Guid Id { get; set; }
}

public class Dog : Animal
{
    public string Name { get; set; }
}

public class Cat : Animal
{
   public string Name { get; set; }
}

我知道属性name应该在Animal中。
现在,当我选择所有实体时:

_context.Animals.ToListAsync();

我得到以下SQL:

SELECT
    "m0"."ID",
    "m0"."NAME",
    "m1"."NAME",
    CASE
        WHEN "m1"."ID" IS NOT NULL THEN N'Dog'
        WHEN "m0"."ID" IS NOT NULL THEN N'Cat' 
    END "Discriminator"
FROM ANIMAL
LEFT JOIN DOG "m0" ON "m0".ID = ANIMAL.ID
LEFT JOIN CAT "m1" ON "m1".ID = ANIMAL.ID

生成的SQL导致Oracle异常错误
ORA-00918:列定义不明确
(因为name列)。
是否有解决方法或修复方法?
使用Oracle.EntityFrameworkCore@6.21.61

fgw7neuy

fgw7neuy1#

我刚刚测试了EF Core 6,EF能够处理这一问题:
上下文:

public class MyContext : DbContext
{
    public DbSet<Animal> Animals { get; set; } = default!;
    public DbSet<Dog> Dogs { get; set; } = default!;
    public DbSet<Cat> Cats { get; set; } = default!;
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder
            .UseOracle("my random connextion string");
    }
}

public abstract class Animal
{
   public Guid Id { get; set; }
}

public class Dog : Animal
{
    public string Name { get; set; } = default!;
}

public class Cat : Animal
{
   public string Name { get; set; } = default!;
}

查询:

var a = db.Animals.ToQueryString();

结果是:

SELECT a.Id, a.Discriminator, a.Name, a.Dog_Name FROM Animals a

相关问题