0

I have a function in SQL that returns a number. This function is called some_number. I also have a DbSet<Table>:

class Table {
    public string Name { get; set; }
}

Now i want to give the Table a property that is a placeholder for a function call:

class Table {
    public string Name { get; set; }
    public SqlFunction<string> some_number { get; set; }
}

If i now query a row in the table, every some_number column is the result of the some_number function in sql.

Is something like that possible? Maybe with some additional linq? Help appreciated.

2
  • EntityFrameworkCore? Commented Apr 24, 2023 at 14:48
  • Not really answering to the question, but as an alternative, you could create a SQL view using this function. DbSet would be easier to write (and I think such a function should not be exposed outside the db). Commented Apr 24, 2023 at 16:15

1 Answer 1

0

You can achieve this in EF CORE with [DbFunction] attribute

public class YourDbContext : DbContext
{
    [DbFunction("dbo", "some_number")]
    public static string SomeMethod() 
    {
      return string.Empty;
    }
}

And use it like

var result = context.Table.Select(t => new Table {
    Name = t.Name,
    some_number = YourDbContext.SomeMethod()
});
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.