EF 存儲過程(下)
本節,我們將學習如何手動添加/修改存儲過程,如何使EF能夠支持Output類型的參數
> 添加/修改存儲過程
有時候,某個SQL語句比較復雜,但是數據庫中又沒有定義相應的存儲過程。這個時候,我們又想使上層代碼比較簡單、方便的方式來完成此項任務。那麽,此時,我們便可以手工在實體模型(.edmx文件)中添加自己需要的存儲過程了。這樣既方便上層調用又方便後期的修改。
以手動修改實體模型edmx文件,添加名為CustomerByCommandText的存儲過程為例。具體步驟如下:
修改實體模型文件,找到ssdl部分,添加如下代碼:
<Function Name="CustomerByCommandText" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo" > <CommandText> select c.* from Customers c,Orders o where c.CustomerID=o.CustomerID </CommandText> </Function>
然後,再找到csdl部分,添加如下代碼:
<FunctionImport Name="CustomerByCommandText" EntitySet="Customers" ReturnType="Collection(NorthwindModel.Customers)"></FunctionImport>
接著,找到msl部分,添加如下代碼:
<FunctionImportMapping FunctionImportName="CustomerByCommandText" FunctionName="NorthwindModel.Store.CustomerByCommandText"/>
最後,在實體模型的.cs文件裏面,添加一個執行此存儲過程的方法,代碼如下:
public global::System.Data.Objects.ObjectResult<Customers> GetCustomerByCommandText() { return base.ExecuteFunction<Customers>("CustomerByCommandText"); }
至此,修改完畢。
現在,我們就可以在代碼使用剛才手工定義的存儲過程了。如下代碼所示:
public void GetCustomerByCmdText() { using (var db = new NorthwindEntities()) { var csts = db.GetCustomerByCommandText().Take(10).Skip(0); foreach (var c in csts) Console.WriteLine(c.CustomerID); } }
其實,關鍵的地方就是CommandText這個部分的內容,它裏面就是要執行的SQL語句。另外,我們可以在修改實體模型emdx文件的同時,我們可以看到所有的實體類查詢的SQL語句命令都可以在edmx文件裏找到,我們都可以進行相應的修改。
> Output類型參數
在實際應用當中,很多時候,我們需要使用output類型的存儲過程參數,以便返回我們需要的值。但是,目前,EF不能直接支持output類型的存儲過程參數。為此,我們需要對實體模型進行修改,以便使其支持output類型的輸出參數。具體過程如下:
在數據庫中建立一個為名的GetNameByCustomerId存儲過程,代碼如下:
CREATE PROCEDURE GetNameByCustomerId @CustomerId varchar(5), @ContactName varchar(30) output AS BEGIN SET NOCOUNT ON; SELECT @ContactName=ContactName FROM Customers WHERE [email protected]; END
然後,開始修改實體模型edmx文件。
先找到ssdl定義的部分,添加如下代碼:
<Function Name="GetNameByCustomerId" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo"> <Parameter Name="CustomerId" Type="varchar" Mode="In" MaxLength="5"></Parameter> <Parameter Name="ContactName" Type="varchar" Mode="Out" MaxLength="30"></Parameter> </Function>
接著,在找到csdl定義的部分,添加如下代碼:
<FunctionImport Name="GetNameByCustomerId"> <Parameter Name="CustomerId" Mode="In" Type="String" MaxLength="5"></Parameter> <Parameter Name="ContactName" Mode="Out" Type="String" MaxLength="30"></Parameter> </FunctionImport>
最後,找到msl定義的部分,添加如下代碼:
<FunctionImportMapping FunctionImportName="GetNameByCustomerId" FunctionName="NorthwindModel.Store.GetNameByCustomerId"></FunctionImportMapping>
至此,實體模型emdx文件修改完畢。
接下來,我們需要在實體模型的.cs文件中,增加相應的調用方法。代碼如下:
public partial class NorthwindEntities1 { //執行GetNameByCustomerId的方法 public void GetNameByCustomerId(string CustomerId, out string ContactName) { ContactName = string.Empty; var Pars = new System.Data.EntityClient.EntityParameter[] { new System.Data.EntityClient.EntityParameter{ ParameterName="CustomerId", DbType=System.Data.DbType.String,Value=CustomerId}, new System.Data.EntityClient.EntityParameter{ParameterName="ContactName", DbType=System.Data.DbType.String, Direction=System.Data.ParameterDirection.Output} }; this.ExecuteNonQuery("GetNameByCustomerId", Pars); ContactName = Pars[1].Value.ToString(); } //輔助方法,執行SQL命令 private void ExecuteNonQuery(string functionName, System.Data.EntityClient.EntityParameter[] parameters) { System.Data.EntityClient.EntityCommand cmd = ((System.Data.EntityClient.EntityConnection)this.Connection).CreateCommand(); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.AddRange(parameters); cmd.CommandText = this.DefaultContainerName + "." + functionName; try { if (cmd.Connection.State != System.Data.ConnectionState.Open) cmd.Connection.Open(); cmd.ExecuteNonQuery(); } catch (System.Exception) { throw; } finally { cmd.Connection.Close(); } } }
現在,所有的修改工作都做完了。接下來,我們就可以在代碼中直接調用此存儲過程了。示例代碼如下:
public void OutputTest() { using (var db = new NorthwindModel.NorthwindEntities1()) { string contactname = string.Empty; db.GetNameByCustomerId("ALFKI", out contactname); Assert.IsTrue(!string.IsNullOrEmpty(contactname)); Console.WriteLine(contactname); } }
至此,我們便可以使用Output類型的輸出參數了
EF 存儲過程(下)