Run custom sql with framework4 entity

I need to get user-specific work tasks from a database.

The filter for all users is different, so you need to make all orders sent to the UK, all in 5 other countries, everyone does all the high-value things, everyone makes an order containing> 10 items, etc.

So, what I came up with this idea (shoot it if you have the best!)

I create a view for each user, and all views return the same data, but the filter is different.

In ado.net, I would do something like this:

string sql = "select * from vwWorkOrders" + userName;
[rest of the ado.net here]

but now I am using ef4 and I was wondering what is equivalent to this type of code.

+3
source share
2 answers

You can use the ExecuteStoreQuery method , as in the following example:

context.ExecuteStoreQuery<vwWorkOrder>(sql);

This method allows you to execute the SQL storage file and get strongly typed results.
If you need to pass some parameters, just pass the required ObjectParameter instances in an ExecuteStoreQuery call.

+9
source

You can use eSQL, which allows a little more flexibility in your queries. You can download LinqPad to play with it in advance.

// whereClause is a string
string query = string.Format("select * from ObjectContext.vwWorkOrders where {0}", whereClause);

Then simply use the EntityConnectionand classes EntityCommandto execute the command and loop the results in a manner not entirely different from ADO.NET.

EDIT: ; , . , :

// userName is a string, ie "Michel"
string query = string.Format("select * from ObjectContext.vwWorkOrders{0}", userName);
+3

Source: https://habr.com/ru/post/1774965/


All Articles