Join to Stored Procedure Using Entity Framework - entity-framework

I have a stored procedure that grabs data recursively. I did a function import in my entity set. I can create a function in my ObjectContext that looks like this:
public ObjectResult<ProviderAccountSetting> GetProviderAccountSettings(long providerAccountId)
{
string functionName = "MyContainer.GetProviderAccountSettings";
ObjectParameter providerAccountIdParameter = new ObjectParameter("providerAccountId", providerAccountId);
ObjectResult<ProviderAccountSetting> results = context.ExecuteFunction<ProviderAccountSetting>(functionName, providerAccountIdParameter);
return results;
}
However, I cannot perform a join with LINQ without getting an error. Is there a way to tell Entity Framework to use the stored procedure whenever I access an entity? I would like my stored procedure to be used any time I grab data for that entity. Furthermore, I want it to work with joins. Does Entity Framework support this type of stored-procedure to table mapping? Otherwise, is there a way to join a function import?

Neither of your requirement is possible. You cannot tell EF to use stored procedure every time when you query data. You must manually call your GetProviderAccountSettings to call stored procedure. You also cannot use join (on database side) when using stored procedures (it is even not possible in SQL directly). If you need to join any data to result set of your stored procedure it must be done directly in the procedure and returned as result set. Otherwise you must execute your stored procedure and joined query separately and join them in linq-to-objects.

Related

How to use stored procedure inside the stored procedure in Postgresql

I have been working on my code for our activity in our major comp sci subject. The task asks to update a certain field in the table in postgresql using stored procedure
I have already create a gettopemp() to retrieved the data in the table, and I want to retrieve the information of gettopemp() to my new stored procedure updatetopemp(). How to use stored procedure inside the stored procedure ???
If you want to pass a function name as a parameter and call that in your code, you'll have to use dynamic SQL.

mvc Entity Framwork, insert using select max() from table

I am developing an App using MV5 and Entity Frameowrk to connect to SQL Server Database
I need to do an Insert where the is getting ID like this.
Select max(id)+1 from table where field_id = #ID.
The idea is to do it in one transaction.
Is that Posible to do it in Entity Framework or I should use a Store Procedure?
EF compiles the C# expressions you provide it into SQL, and then executes the resulting SQL. So you can write such a query in LINQ, which will in-fact be computed during a single "call" to the database:
long id = MyDbContext.MyDbSet.Max(entity => entity.Id) + 1;
Do note, that if you are doing this so that you can assign a non existing ID to a new entry- there's no need. EF takes care of this for you, so long as the field is named Id and it is of type long. Just do not assign it any value.

How to create and call stored procedure thorugh Code First approach into EF

How to call stored procedure through code first appraoch/DBCONTEXT file.
In code first appraoch, we can create tables and relationship through Model object but, have to create procedures manually into database or any approach avaibale to generate ?
How to call procedure ?
Thank You

EF5 - modify generated insert/update statement before SaveChanges()

I need to modify the generated SQL for insert/update operation, before it is sent to database. The required modification is very specific, so I was hoping that there is a way to simply append string to statement.
For example, SQL looks like this (Oracle BTW):
UPDATE TABLE_A
SET DESCRIPTION = "ABC"
WHERE OBJECTID = 1
But I want to append this line (in SET part) to update one more field:
SHAPE = sde.st_geometry('point (18 57)', 4326)
I can't add SHAPE column to EF model, because that is unsupported data type.
Now, is there a way I can modify EF generated SQL statement?
You could move this update to a simple stored procedure that is mapped into your entity data model.

map stored procedure to entity returning temproray table data

I have a stored procedure that returns the temporary table data. because i have used dynamic queries. When i tried to map stored procedure using complex types it returns no columns
how to handle temporary table columns name in complex types?
It is not supported by default because EF always executes SET FMTONLY ON before executing your stored procedure. This option will turn off logic execution - it will only ask for metadata but if logic execution is turned off no temporary table is created and no column's metadata exists.
There are some workarounds.