Visual Studio踩踏Linq存储过程

时间:2009-07-18 22:23:16

标签: c# linq-to-sql

我有几个存储过程返回强类型结果集。 我已经了解到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 个答案:

答案 0 :(得分:2)

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

MyDataContext.cs

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

public class Note...

答案 1 :(得分:1)

不要更改生成的代码。如果这样做,那么每次查看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
    }
}
相关问题