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

Unable to use DbFunction in 3.0 #1747

Closed
cwoolum opened this issue Sep 18, 2019 · 5 comments
Closed

Unable to use DbFunction in 3.0 #1747

cwoolum opened this issue Sep 18, 2019 · 5 comments
Assignees
Milestone

Comments

@cwoolum
Copy link

cwoolum commented Sep 18, 2019

I am trying to use DatePart in EfCore 3.0 but it seems to fail due to the dbo schema being prepended to the function name.

Steps to reproduce

public class AnalyticsDbContext : DbContext
{
    public AnalyticsDbContext(DbContextOptions options) : base(options)
    {
    }

    public DbSet<AggregatedMetric> AggregatedMetrics { get; set; }

    [DbFunction("DatePart", Schema = "")]
    public static int? DatePart(string datePartArg, DateTime? date) => throw new Exception();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);   
    }
}

I always get the error {"Cannot find either column \"dbo\" or the user-defined function or aggregate \"dbo.DatePart\", or the name is ambiguous."} even though my definition shows an empty schema.

I would expect that the DbFunctionAttribute would be respected and the schema would be blank. I halve also tried to use the fluent API to register with the same problem

modelBuilder
    .HasDbFunction(typeof(DateExtensions).GetMethod(nameof(DateExtensions.DatePart)))
    .HasName("DatePart")
    .HasSchema("");

Further technical details

EF Core version: 3.0.0-rc1
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET Core 3.0)
Operating system:
IDE: (e.g. Visual Studio 2019 16.3)

@smitpatel smitpatel self-assigned this Sep 18, 2019
@smitpatel smitpatel transferred this issue from dotnet/efcore Sep 18, 2019
@smitpatel
Copy link
Contributor

@cwoolum - In EF Core 3.0, we removed assumption that empty schema means built-in function. So null or empty string as schema means it's default schema which would be dbo. in order to configure to use built in function it requires you to map custom translation like follows.

            modelBuilder
                .HasDbFunction(typeof(MyContext).GetMethod(nameof(MyContext.DatePart)))
                .HasTranslation(args => SqlFunctionExpression.Create("DatePart", args, typeof(int?), null));

Note that above would also fail because datepartarg will be printed as string which is not valid in SqlServer. The custom translation also need to take care of it but currently blocked by dotnet/efcore#17918

Overall for this scenario see dotnet/efcore#17268 for various ways we plan to improve this experience.

@smitpatel
Copy link
Contributor

I transferred this issue here to document the way to map to a built-in function which is different from #504

@smitpatel
Copy link
Contributor

Also document the breaking change that null or empty schema means default schema defined in model.

@AndriySvyryd
Copy link
Member

@smitpatel The breaking change note should be added ASAP

smitpatel added a commit that referenced this issue Sep 20, 2019
smitpatel added a commit that referenced this issue Sep 20, 2019
#1759)

* Add breaking change for DbFunction.Schema empty string not a built-in function anymore

Part of #1747
@ajcvickers ajcvickers removed this from the 3.1.0 milestone Jan 15, 2020
@ajcvickers
Copy link
Member

@smitpatel Duplicate of #500?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants