How can I call a MYSQL SP from EF4
I tried to add the SP the edmx file, but the input parameters are not shown. I tried to use the code below, but it errors out.
IEnumerable<Finance> userFinance =
((IObjectContextAdapter)this)
.ObjectContext.ExecuteStoreQuery<Finance>("GetFinanceContent @userId", parameter);
I also tried:
SqlParameter parameter = new SqlParameter
{
DbType= DbType.Int32,
Direction = ParameterDirection.Input,
Value= 100,
ParameterName ="userId"
};
var results= db.Database.ExecuteSqlCommand ("GetFinanc开发者_StackOverfloweContent @userId", parameter);
This gives some parameters error. Only MySqlParameter objects may be stored
Woking with stored procedure in EF4 is so much easy. Here is no need to add Sp in edmx file you can get directly access to it just you have to use function mapping just follow these rules ...
Update your model and add stored procedure in it.
now in model browser view go to Stored procedure folder you will see your added SP in it.
Now select SP and select the option "Create Function Import" by doing Right Click.
Now Function import Window would be open in it in the Return Type Section select the return type according to the data your SP returning..
--> If your SP is returning exact match of an entity select that entity .If a scalar value then select its return type if not then you need to create an entity that exact match your SP result.
After that you can get access to SP like this.
using(EFEntity ef = new EFEntity ()) { var results = from result in ef.YourSpName(Param1, param2,..) select result }
Hope this will help you ..
Updated answer: As you are saying you are not getting parameters it means that here is no parameter defined in your SSDL so you have to do this mannualy.
Here is the solution of your problem.
Good Luck
精彩评论