简体   繁体   中英

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 ("GetFinanceContent @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...

  1. Update your model and add stored procedure in it.

  2. now in model browser view go to Stored procedure folder you will see your added SP in it.

  3. Now select SP and select the option "Create Function Import" by doing Right Click.

  4. 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.

  5. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM