2014-03-29 43 views
0

我在下面所示的存儲過程:如何使用從存儲過程中的實體「選擇」的結果FrameWork的

public class Save_Customer_MyResult 
{ 
    public int Identity { get; set; } 
} 

那麼我下面插入:

ALTER PROCEDURE [dbo].[Save_Customer](@PK int , @Name nvarchar(30) , @Address nvarchar(100) , @CustomerGrp_FK int) 
AS 
SET NOCOUNT ON; 
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; 
BEGIN TRAN 
BEGIN TRY 
    IF (@PK = -1) 
    BEGIN 
     INSERT INTO dbo.[Customer] 
     SELECT @Name , @Address , @CustomerGrp_FK 

     SELECT SCOPE_IDENTITY() as IdentityPK 
    END 
    ELSE 
    BEGIN 
     UPDATE Customer 
     SET Name = @Name ,Address = @Address ,CustomerGrp_FK = @CustomerGrp_FK 
     WHERE PK= @PK 
    END 

要使用SELECT SCOPE_IDENTITY() as IdentityPK,我下面的類創建代碼在我的EntityContext類中:

public DbSet<Save_Customer_MyResult> Save_CustomerDB { get; set; } 

public virtual Save_Customer_MyResult Save_Customer1(Nullable<int> pK, string name, string address, Nullable<int> customerGrp_FK) 
    { 
     return this.Database.SqlQuery<Save_Customer_MyResult>("Save_Customer @PK, @Name, @Address, @CustomerGrp_FK", new System.Data.SqlClient.SqlParameter("PK", pK) 
      , new System.Data.SqlClient.SqlParameter("Name", name) 
      , new System.Data.SqlClient.SqlParameter("Address", address) 
      , new System.Data.SqlClient.SqlParameter("CustomerGrp_FK", customerGrp_FK)).First<Save_Customer_MyResult>(); 
    } 

現在在我的表示層。當我打電話給方法。所述Identity字段根據this article是0 :(

Customer.Save_Customer_MyResult scr=customercontext.Save_Customer1(-1, txt_Name.Text, txt_Address.Text, Int32.Parse(cmb_GroupName.SelectedValue.ToString())); 

     MessageBox.Show(scr.Identity .ToString()); 

回答

1

1-我只是做了新Complex Type

2- stored procedure mappingentity

enter image description here

3-然後。:

IEnumerable<Customer.Save_Customer_Result > s= customercontext.Save_Customer(-1, txt_Name.Text, txt_Address.Text,Int32 .Parse(cmb_GroupName.SelectedValue.ToString())); 
     MessageBox.Show(s.First<Customer.Save_Customer_Result>().IdentityPK .ToString()); 
相關問題