【问题标题】:How to avoid updating EntityModel.Context for existing Stored Procedure?如何避免为现有存储过程更新 EntityModel.Context?
【发布时间】:2017-02-02 07:55:26
【问题描述】:

我正在开发一个 Windows 应用程序。我有一个从数据库生成的模型类型的实体模型。我首先导入了一些存储过程,对于一个 SP,我将上下文替换数据类型从 bool 更改为 int。现在,当我尝试更新模型(添加 SP)时,更改后的 SP 的上下文将返回其默认类型,即 bool 导致错误(函数具有无效参数)。

这是模型的 Context.cs。在这里,当我将类型从 bool 更改为 int 时,应用程序工作正常,但是当我更新模型时,类型又恢复为 bool 并抛出错误。如何避免这种情况?

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 类型。

仅供参考:这是我的 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 =@tranno and year_code=@yearcode 
commit tran

我不想每次更新模型时都更改该 SP 的上下文。有什么解决办法??为什么即使类型是 int,模型也会将类型提取为 bool?

【问题讨论】:

  • 您的意思是boolbyte
  • @sefe 对不起.. 是字节..

标签: c# winforms entity-framework stored-procedures


【解决方案1】:

您的存储过程使用tinyintsmallint 作为参数类型。 EF 在存储过程的访问器方法中反映了这一点,并使用 Nullable&lt;T&gt; 包装的各自类型 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);
}

【讨论】:

  • 我很不明白第二点。您是在告诉我在上下文类中创建一个方法吗?那岂不是模棱两可?即使我将方法更改为私有(您是说不要),它也会在下一次更新时公开,对吧?
  • 不会有歧义,这是正常的过载。然后您将其更改为私有在视觉设计器中。然后它不会覆盖。您无法触摸自动生成的代码。您可以在可视化设计器中进行编辑,也可以编辑自己的部分上下文类。
猜你喜欢
  • 2019-12-06
  • 1970-01-01
  • 1970-01-01
  • 2018-04-12
  • 2020-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多