2017-02-02 24 views
1

我正在開發一個Windows應用程序。我有一個模型類型的實體模型從數據庫生成。我首先導入了一些存儲過程,並且對於一個SP,我已經將上下文替換數據類型從bool更改爲int。現在,當我嘗試更新模型(添加SP)時,更改後的SP的上下文將恢復爲其默認類型,即bool導致錯誤(函數具有無效參數)。如何避免更新現有存儲過程的EntityModel.Context?

這是模型的Context.cs。在這裏,當我從布爾類型更改爲詮釋,應用程序工作正常,但是當我更新模型的類型是回到布爾和它的拋出錯誤。如何避免這種情況?

public virtual ObjectResult<Web_get_obj_details_Result> Web_get_obj_details(Nullable<byte> hcode, Nullable<byte> vcode, Nullable<byte> yearcode, Nullable<short> tranno) 
     { 
      var hcodeParameter = hcode.HasValue ? 
       new ObjectParameter("hcode", hcode) : 
       new ObjectParameter("hcode", typeof(byte)); 

      var vcodeParameter = vcode.HasValue ? 
       new ObjectParameter("vcode", vcode) : 
       new ObjectParameter("vcode", typeof(byte)); 

      var yearcodeParameter = yearcode.HasValue ? 
       new ObjectParameter("yearcode", yearcode) : 
       new ObjectParameter("yearcode", typeof(byte)); 

      var trannoParameter = tranno.HasValue ? 
       new ObjectParameter("tranno", tranno) : 
       new ObjectParameter("tranno", typeof(short)); 

      return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<Web_get_obj_details_Result>("Web_get_obj_details", hcodeParameter, vcodeParameter, yearcodeParameter, trannoParameter); 
     } 

我是多麼訪問SP:

IList<Web_get_obj_details_Result> ObjWeb_get_obj_details_Result = EntityObj.Web_get_obj_details(HobliCode, VillageCode, YearCode,TranNo).ToList<Web_get_obj_details_Result>(); 

這裏HobliCode,VillageCode,YearCode,TranNo都是int類型。

FYI:這是我的SP

ALTER PROCEDURE [dbo].[Web_get_obj_details] 
@hcode as tinyint , @vcode as tinyint , @yearcode as tinyint , @tranno as smallint 
as 
declare @dcode as integer , @tcode as integer 
begin tran 
    select @dcode = dist_code , @tcode = taluk_code from mst_config 
    select convert(varchar(10),notice_sdate,103) as notice_sdate ,convert(varchar(10),obj_date,103) as obj_date,objector_name,obj_details from tr_objection 
    where dist_code= @dcode and taluk_code = @tcode and hobli_code = @hcode and village_code = @vcode and tran_no [email protected] and [email protected] 
commit tran 

我不想改變SP的我每次更新模型的上下文。任何解決方案以及爲什麼模型將類型作爲bool提取,即使類型是int?

+0

你的意思是'bool'或'byte'? – Sefe

+0

@sefe抱歉..它是字節.. – Rakesh

回答

1

您的存儲過程使用tinyintsmallint作爲參數類型。 EF反映了存儲過程的訪問方法,並生成了包含在Nullable<T>中的相應類型byteshort(又名ByteInt16)的方法。

EF數據庫中的黃金法則是:不要修改生成的代碼。它會在下次更新時被覆蓋。

您有兩個選項來解決問題:

  1. 更改您的存儲過程使用int類型的參數,並更新從數據庫模型。訪問方法將使用int(又名Int32)。
  2. 在上下文的自定義擴展中創建包裝方法。您可以將自動生成的方法設置爲私有的(通過設計人員 - 切記不要修改自動生成的代碼)以防止直接訪問。

這應該是這樣的:

public ObjectResult<Web_get_obj_details_Result> Web_get_obj_details(int hcode, int vcode, int yearcode, int tranno) 
{ 
    return Web_get_obj_details((byte)hcode, (byte)vcode, (byte)yearcode, (short)tranno); 
} 
+0

我完全不理解第二點。你告訴我要在上下文類中創建一個方法?這不會是模糊的嗎?即使我改變方法爲私人(你說不要)它會公佈在下一次更新的權利? – Rakesh

+0

它不會含糊不清,這是一個正常的過載。然後你將它改變爲視覺設計師的私人空間。然後它不會寫。您無法觸摸自動生成的代碼。您可以在視覺設計器中進行編輯,並且您可以編輯自己的部分上下文類。 – Sefe

相關問題