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

Getting errors when mixing DbFunction and SqlQueryRaw #32680

Closed
ErikEJ opened this issue Dec 27, 2023 · 3 comments · Fixed by #32790
Closed

Getting errors when mixing DbFunction and SqlQueryRaw #32680

ErikEJ opened this issue Dec 27, 2023 · 3 comments · Fixed by #32790
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Milestone

Comments

@ErikEJ
Copy link
Contributor

ErikEJ commented Dec 27, 2023

Running this console app with a valid connection string causes the following runtime error:

The entity type 'tvpResult' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types, see https://go.microsoft.com/fwlink/?linkid=2141943.

Removing OnModelCreating makes no difference.

using Microsoft.EntityFrameworkCore;

var context = new TestContext(new DbContextOptionsBuilder<TestContext>().UseSqlServer("Server=.\\SQLEXPRESS;Database=Northwind;Trusted_Connection=True;Encrypt=false").Options);

var result = await context.Database
    .SqlQueryRaw<RawResult>("SELECT Name from sys.databases")
    .OrderBy(d => d.Name)
    .ToListAsync();

foreach (var item in result)
{
    Console.WriteLine(item.Name);
}


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

    [DbFunction("tvp", "dbo")]
    public IQueryable<tvpResult> tvp(int? storeid)
    {
        return FromExpression(() => tvp(storeid));
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<tvpResult>().HasNoKey();
    }
}

public partial class tvpResult
{
    public int Id { get; set; }
    public string Name { get; set; } = default!;
    public decimal Total { get; set; }
}

public partial class RawResult
{
    public string? Name { get; set; }
}
CREATE FUNCTION [dbo].[tvp] (@storeid int)
RETURNS TABLE
AS
RETURN
(
    SELECT 1 AS Id, 'Name' AS [Name], 0.666 AS 'Total'
);
GO

Include stack traces

The entity type 'tvpResult' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types, see https://go.microsoft.com/fwlink/?linkid=2141943.
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateNonNullPrimaryKeys(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal.SqlServerModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelRuntimeInitializer.Initialize(IModel model, Boolean designTime, IDiagnosticsLogger`1 validationLogger)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.AdHocMapper.AddEntityType(Type clrType)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.AdHocMapper.GetOrAddEntityType(Type clrType)
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.SqlQueryRaw[TResult](DatabaseFacade databaseFacade, String sql, Object[] parameters)
   at Program.<<Main>$>d__0.MoveNext() in C:\Users\xxx\source\repos\ConsoleApp13\Program.cs:line 5

Include provider and version information

EF Core version: 8.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0
Operating system:
IDE: Visual Studio 2022 17.8

@ErikEJ
Copy link
Contributor Author

ErikEJ commented Jan 8, 2024

@ajcvickers Thoughts?

@ajcvickers
Copy link
Member

@ErikEJ Thanks for the ping--I missed this before. Looks like a bug.

@ajcvickers ajcvickers added this to the 8.0.x milestone Jan 11, 2024
ajcvickers added a commit that referenced this issue Jan 11, 2024
Fixes #32680

Two follow on items after this is merged:
- Port to 8
- Make AdHocMapper public
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jan 11, 2024
ajcvickers added a commit that referenced this issue Jan 13, 2024
Fixes #32680

Two follow on items after this is merged:
- Port to 8
- Make AdHocMapper public
@ajcvickers ajcvickers reopened this Jan 13, 2024
ajcvickers added a commit that referenced this issue Jan 13, 2024
Fixes #32680

Two follow on items after this is merged:
- Port to 8
- Make AdHocMapper public
ajcvickers added a commit that referenced this issue Jan 16, 2024
…32807)

* Don't discover DbFunctions when building ad-hoc model (#32790)

Fixes #32680

Two follow on items after this is merged:
- Port to 8
- Make AdHocMapper public

* Quirk
@ajcvickers ajcvickers modified the milestones: 8.0.x, 8.0.2 Jan 16, 2024
@ErikEJ
Copy link
Contributor Author

ErikEJ commented Jan 16, 2024

Thanks, @ajcvickers !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants