代码之家  ›  专栏  ›  技术社区  ›  Kenny Mann

Visual Studio Stomping on Linq存储过程

  •  1
  • Kenny Mann  · 技术社区  · 15 年前

    我有几个返回强类型结果集的存储过程。 我了解到Linq有自己的处理方法,它必须被重写(或者至少看起来是这样)。

    我的问题是Visual Studio有时坚持强制重新创建存储过程。我想禁用这个。

    这是我手动修改的文件:

        [Function(Name="dbo.spGetNote")]
        public ISingleResult<Note> spGetNote([Parameter(DbType="Int")] System.Nullable<int> noteId, [Parameter(DbType="Int")] System.Nullable<int> securityUserId)
        {
            IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), noteId, securityUserId);
            return ((ISingleResult<Note>)(result.ReturnValue));
        }
    

    以下是默认值:

        [Function(Name="dbo.spGetNote")]
        public ISingleResult<spGetNoteResult> spGetNote([Parameter(DbType="Int")] System.Nullable<int> noteId, [Parameter(DbType="Int")] System.Nullable<int> securityUserId)
        {
            IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), noteId, securityUserId);
            return ((ISingleResult<spGetNoteResult>)(result.ReturnValue));
        }
    

    这是较小的一个。

    它还涉及其他一些领域,但这些领域是可以修复的。它得到 真实的 旧的回去调整这个。

    我们最后要做的是,为返回自己强类型项的每个存储过程提供它自己的数据上下文/类,这样每次更新DAL时,它(Visual Studio)都不会踩在自定义更改上。

    我能做些什么来缓解这种头痛吗?

    带来这些的是我正在清理名称空间,我发现如果没有Visual Studio拆分项目中的每个存储过程,我就无法更改命名空间,我不想花费数小时清理这些混乱。似乎全局替换是不够的,因为Visual Studio检测到这一点,然后说它找不到连接字符串,并且 必须 重新生成涉及的每个文件。

    2 回复  |  直到 15 年前
        1
  •  2
  •   Joe Mayo    15 年前

    由于自动生成的DataContext是分部的,所以可以创建自己的分部类,并将自定义的方法/类型移到分部中。即

    mydataContext.cs版

    public partial MyDataContext
    {
            [Function(Name="dbo.spGetNote")]        
            public ISingleResult<Note> spGetNote([Parameter(DbType="Int")]...
    }
    
    public class Note...
    

        2
  •  1
  •   Marc Gravell    15 年前

    不要更改生成的代码。如果这样做,那么在查看DBML文件时,所做的更改很可能会丢失。你 可以 (我没有尝试过)可以通过手工编辑DBML来解决这个问题(它只是XML);但是,IMO,处理这个问题的最简单方法是在存储库中,从DBML生成的类型到您的类型进行投影:

    Note[] SomeFunc(...) {
        using(var ctx = ...) {
            return (from row in ctx.SomeSP(...) // row is the dbml type
                 select new Note { // Note is our custom type
                     Id = row.Id,
                     Name = row.Name,
                     // etc
                  }).ToArray(); // or whatever
        }
    }