简体   繁体   中英

late-bound stored procedure execution with Entity Framework

I'm looking for a way to do something like this:

var  db = new DALEntities();
db.StoredProcedureToCall();

but, I want it to be dynamic, in that I won't know WHICH call to make until after some other data is delivered to me.

var db = new DALEntities();
db[tSproc]();

I know I could have a switch statement test the value of tSproc, and then call as above, but is there a more elegant way?

Thanks

Put your cursor on db.StoredProcedureToCall() and press F12. You'll see the code that EF generated is already dynamic.

Depending on the EF version you use, it will look something like:

public ObjectResult<OrderDetail> GetDetailsForOrder
 (Nullable<global::System.Int32> orderid)
{
  ObjectParameter orderidParameter;
  if (orderid.HasValue)
  {
    orderidParameter = new ObjectParameter("orderid", orderid);
  }
  else
  {
    orderidParameter = new ObjectParameter("orderid", typeof(global::System.Int32));
  }
  return base.ExecuteFunction<OrderDetail>("GetDetailsForOrder", orderidParameter);
}

Just write your code similarly. You don't need a switch.

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