1

I am having a lot of trouble trying to run a SQL function from the DbContext, I have tried several suggestions with no luck.

I have a class library project, with Entity Framework Core (3.1.5). The project compiles and runs but when I try to run a unit test (before I deploy or integrate) every time I touch the function I get an error

System.NotImplementedException: 'The method or operation is not implemented.'

This is the SQL function:

CREATE FUNCTION [dbo].[GetLocalDate]()
RETURNS DATETIME
AS
BEGIN
     DECLARE @gd AS DATETIME =  getUTCdate()
     DECLARE @D AS datetimeoffset

     SET @D = CONVERT(datetimeoffset, @gd) AT TIME ZONE 'Central Standard Time'
     RETURN CONVERT(datetime, @D);
END

which is working in the database.

This is my database context:

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

    [DbFunction("GetLocalDate", "dbo")]
    public static DateTime GetLocalDate()
    {
        throw new NotImplementedException();
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.HasDbFunction(() => VendorDbContext.GetLocalDate());
    }
}

and this is the call in the class - Context is a local variable of the VendorDbContext type, and Vendor is an existing valid entity in the model:

var now = Context.Vendor.Where(v => v.CompanyId == 6)
                        .Select(v => VendorDbContext.GetLocalDate() ).FirstOrDefault();

Does not matter what I try, I always got the exception mentioned above.

Any ideas please?

6
  • When debugging I can reach the line in the function that throws the exception, so I am guessing is not properly mapped but I can tell why Commented Jul 30, 2020 at 23:21
  • So you're getting the exception when running a unit test? Are you mocking the DbContext or using an actual implementation? Also, do you get the exception when running the application normally? Commented Jul 30, 2020 at 23:42
  • Hi thanks so much for asking, the context is a EF empty context using (VendorDbContext context = new VendorDbContext(options)) and i havent run it normally, until unit testing complete i am not supposed to integrate this. Commented Jul 31, 2020 at 15:06
  • now with that question i think is important to add that the unit testing is in memory database Commented Jul 31, 2020 at 18:46
  • yeah I'm guessing that's the crux of the matter. It seems that the in-memory provider is rather limited functionality, so the DbFunction is likely just not executing as it would against a live database Commented Jul 31, 2020 at 23:05

1 Answer 1

1

I faced same problem. I found reason of this problem for my code. If Entity Framework didn't execute LINQ query and occured local evalution, your DbFunction will execute in C# side. Therefore VendorDbContext.GetLocalDate() called in C# side will throw System.NotImplementedException

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.