一则Entity Framework 学习中的问题

以前都是在用 Linq2sql来作 orm 的,但最微软对 Linq2sql 不再作升级,但又因为 entity framework 和 linq2sql 有很多的相似之处,也就作一下学习。

体会下 EF 大体上和 linq 差不多,基本的增删改等 操作都相似。在功能是执行效率上 EF要好于linq 但有个地方不如 linq2sql 了,在linq2sql 中存储过程是直接映射成方法的,但EF中就没那么方便了。

 

自己写了个存储过程:

GetNewBillCode

目地是为了生成订单号,

如 OD-001-2011-04-06-001

但在操作中 怎么 new 也 出来这个存储过程来。 原因在于EF对 存储过程支持的不是很好。

所以要自己通方法来实现存储过程。

该存储过程有三个参数和一个还回值参数

shopid:部门

billDate:订单日期

mastTableName:表名称

out billCode:还回订单编码

 

自己写个方法来实现它

 

   1:          public string GetNewBillCodeByDate(int shipid,string billDate,string mastTableName,out string billCode)
   2:          {
   3:              var pars = new System.Data.EntityClient.EntityParameter[] 
   4:              {
   5:                  //分支机构
   6:                  new System.Data.EntityClient.EntityParameter{ ParameterName="shopid", DbType=System.Data.DbType.Int32,Value=shipid},
   7:                  //订单日期
   8:                  new System.Data.EntityClient.EntityParameter{ ParameterName="billDate", DbType=System.Data.DbType.String,Value=billDate},
   9:                  //表名称
  10:                  new System.Data.EntityClient.EntityParameter{ ParameterName="mastTableName", DbType=System.Data.DbType.String,Value=mastTableName},
  11:                  //订单编码
  12:                  new System.Data.EntityClient.EntityParameter{ParameterName="billCode", DbType=System.Data.DbType.String, Direction=System.Data.ParameterDirection.Output,Size = 50}
  13:   
  14:              };
  15:   
  16:              ExecuteFunction("getnewbillcodebydate", pars);
  17:              return billCode= pars[3].Value.ToString();
  18:          }

 

   1:          //执行存储过程
   2:          public void ExecuteFunction(string functionName, System.Data.EntityClient.EntityParameter[] parameters)
   3:          {
   4:              System.Data.EntityClient.EntityCommand cmd = ((System.Data.EntityClient.EntityConnection)this.Connection).CreateCommand();
   5:              cmd.CommandType = System.Data.CommandType.StoredProcedure;
   6:              cmd.Parameters.AddRange(parameters);
   7:              cmd.CommandText = this.DefaultContainerName + "." + functionName;
   8:              try
   9:              {
  10:                  if (cmd.Connection.State != System.Data.ConnectionState.Open)
  11:                      cmd.Connection.Open();
  12:                 // var obj =
  13:                      cmd.ExecuteScalar();
  14:                  //return obj;
  15:              }
  16:              catch (System.Exception)
  17:              {
  18:                  throw;
  19:              }
  20:              finally
  21:              {
  22:                  cmd.Connection.Close();
  23:              }
  24:          }

 

上网Copy 了些 高手们的代码,虽然代码是有了,但执行的时候却还是出错了

 

最后Google 了下

在一篇文章里找到了答案

http://archive.cnblogs.com/a/1804358/

摘录了他的最后一名话 问题成功解决

总结:如果不使用最后SELECT结果的话,则不必理会返回类型,将返回类型设置为“”即可,当然我们封装的函数可以获取存储过程Return结果及OUTPUT类型的参数。所以上面例子代码可以删除存储过程中的Return前的Select语句,同时添加函数导入时指定返回类型为“无”。

补充:

虽然我们从数据库来生成 EF模型时 ,可以把 存储过程 托过去,但这时 还不能用它

image

就比如,它导入的 存储过程 是在 “存储过程” 中的,而不是在 “函数导入” 中的

这是我们要作的是 添加 函数导入  类型为“无”

 

image

 

 

image

posted @ 2011-04-06 21:17  wunaigong  阅读(344)  评论(0编辑  收藏  举报