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?
DbFunctionis likely just not executing as it would against a live database