Last post Jun 13, 2015 07:57 AM by Zhi Lv - MSFT
Jun 11, 2015 02:54 PM|maggiemays|LINK
I can't see a scalar function appearing in my entity framework.
How can I use it? I also saw that I should try to use context.executescalar, but I don't have that option either.
Can somebody give me some tips on why this might be?
Jun 11, 2015 03:30 PM|Rion Williams|LINK
Do you have an example of the existing code that you are trying to use?
It might make it much easier to provide you with a helpful answer if we had an idea of exactly what type of function or query you were trying to execute and had some context of how you were attempting to do so.
Jun 11, 2015 04:08 PM|maggiemays|LINK
The scalar function takes in a date and then creates a key based on the date and then generates a number. That makes up the key.
When I wasn't using the entity model, i just would call the stored procedure that included the function ( which returned this string value) and then used it in the insert function.
But, I thought since I'm not using that stored procedure, I need to access that function to create the key. I hope that makes sense. thanks.
Jun 13, 2015 07:57 AM|Zhi Lv - MSFT|LINK
From your description, it seems that you want to Scalar Function with Entity Framework, if that is the case, please refer to the following code:
CREATE FUNCTION [dbo].[HeyYou] ()
RETURN 'Hey this works'
Code in page:
protected void Page_Load(object sender, EventArgs e)
using (MayEntities context = new MayEntities())
string str = context.Database.SqlQuery<string>("select dbo.HeyYou()").Single().ToString();
Response.Write(str); //output:'Hey this works'
For more details, please refer to this link:
Entity Framework Code-First Execute Scalar-Valued Functions
How to use scalar-valued function with linq to entity? [duplicate]