Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong query generated for mapped function when using schema #359

Open
nfdavi opened this issue Jan 23, 2024 · 1 comment
Open

Wrong query generated for mapped function when using schema #359

nfdavi opened this issue Jan 23, 2024 · 1 comment
Labels

Comments

@nfdavi
Copy link

nfdavi commented Jan 23, 2024

This issue has been previously reported as #255 and is still present in Oracle.EntityFrameworkCore 8.21.121

when schema is explicitly specified in OnModelCreating, using function mapping results in incorrectly generated query - schema is placed before table (it should be inside).

The generated (incorrect) query:

SELECT "t"."Id", "t"."Data"
FROM "DEV3".table("TestFunction"()) "t"

should be instead:

SELECT "t"."Id", "t"."Data"
FROM table("DEV3"."TestFunction"()) "t"

reproducible example (.net core 8.0):

// Program.cs
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddDbContext<TestContext>(
    opt => opt.UseOracle(builder.Configuration
        .GetConnectionString("DefaultConnection")),
    contextLifetime: ServiceLifetime.Scoped);

var app = builder.Build();

using var scope = app.Services.CreateScope();
var ctx = scope.ServiceProvider.GetService<TestContext>()!;
var result = ctx.TestFunction().ToList(); // generates incorrect query which produces ORA-00903

app.Run();
// model + context
public class TestTable
{
    public int Id { get; set; }
    
    public string Data { get; set; }
}

public class TestContext : DbContext
{
    public TestContext(DbContextOptions<TestContext> options)
        : base(options)
    {
    }

    public DbSet<TestTable> TestTables { get; set; }
    
    public IQueryable<TestTable> TestFunction() => FromExpression(() => TestFunction());

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("DEV3"); // this causes the issue - when schema is not specified, the query is generated correctly

        modelBuilder.Entity<TestTable>(builder =>
        {
            builder.HasKey(b => b.Id);
        });
        
        modelBuilder.HasDbFunction(typeof(TestContext).GetMethod(nameof(TestFunction)))
            .HasName("TestFunction");
    }
}

DB can be created using ef.core migrations

dotnet ef migrations add IniticalCreate
dotnet ef database update

function definition in Oracle:

CREATE OR REPLACE TYPE "TestType" AS OBJECT (
    "Id" NUMBER,
    "Data" NVARCHAR2
);

/
CREATE OR REPLACE TYPE "TestTableType" AS TABLE OF UserRecordType;
/
CREATE OR REPLACE FUNCTION "TestFunction"()
RETURN "TestTableType" PIPELINED
IS
BEGIN
    FOR "tt" IN (
       SELECT * FROM "TestTables"
    ) LOOP
        PIPE ROW("TestType"(
            "tt"."Id",
            "tt"."Data"
        ));
    END LOOP;
    RETURN;
END;
@alexkeh
Copy link
Member

alexkeh commented Jan 23, 2024

Thanks @nfdavi for reporting! I was able to reproduce your error and have filed a bug (36218521) to have the dev team review the issue and provide a fix.

@alexkeh alexkeh added the bug label Feb 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants