生成的DBML代码错误地将返回的列标记为存储过程的NOT NULL

时间:2010-08-13 08:29:57

标签: c# sql-server linq linq-to-sql

我在SQL Server中有一个存储过程,它通过select:

返回一些数据
CREATE PROCEDURE spDDIs_Get(@ID INT) AS

IF NOT EXISTS (SELECT ID FROM tDDIs WHERE ID = @ID)
    RAISERROR('ICT0005:DDI does not exist', 18, 1)

ELSE
    SELECT D.ID, D.SchemeID, C.ID ConfigurationID, C.Description Configuration,
           D.RecordCall, D.DDINumber
      FROM tDDIs D
      LEFT JOIN tConfigurations C ON C.ID = D.ConfigurationID
     WHERE D.ID = @ID

此过程返回的ConfigurationID列在某些情况下返回NULL。

但是,在Visual Studio中,当我将此存储过程从服务器资源管理器拖到我自动生成用于LINQ的代码的DBML文件中时,它会错误地将此列设置为不可为空的变量类型:

    public partial class spDDIs_GetResult
{

    private int _ID;        
    private int _SchemeID;      
    private int _ConfigurationID;       
    private string _Configuration;      
    private bool _RecordCall;       
    private string _DDINumber;

    public spDDIs_GetResult()
    {
    }

    [Column(Storage="_ID", DbType="Int NOT NULL")]
    public int ID
    {
        get
        {
            return this._ID;
        }
        set
        {
            if ((this._ID != value))
            {
                this._ID = value;
            }
        }
    }

    [Column(Storage="_SchemeID", DbType="Int NOT NULL")]
    public int SchemeID
    {
        get
        {
            return this._SchemeID;
        }
        set
        {
            if ((this._SchemeID != value))
            {
                this._SchemeID = value;
            }
        }
    }

    [Column(Storage="_ConfigurationID", DbType="Int NOT NULL")]
    public int ConfigurationID
    {
        get
        {
            return this._ConfigurationID;
        }
        set
        {
            if ((this._ConfigurationID != value))
            {
                this._ConfigurationID = value;
            }
        }
    }

            // .... rest of the class omitted ....

}

我需要定义:

private int? _ConfigurationID;

private int _ConfigurationID;

该方法的定义还有一个标记为Int NOT NULL的属性。我不介意在生成的代码中手动修复它,但是如果我需要对存储过程进行更改并重新导入它,那么变量将返回到非可空状态,这会导致错误,这真的很痛苦。保持更新。

如何才能正确导入并允许空值,或确保我的编辑后的代码不会被覆盖?有没有办法在SQL Server中向存储过程添加属性以将列标记为可为空?

谢谢, 本

1 个答案:

答案 0 :(得分:1)

我自己解决了这个问题。我从查询中的错误表中获取列。如果我将查询更改为此,则可以:

        SELECT D.ID, D.SchemeID, D.ConfigurationID, C.Description Configuration,
           D.RecordCall, D.DDINumber
      FROM tDDIs D
      LEFT JOIN tConfigurations C ON C.ID = D.ConfigurationID
     WHERE D.ID = @ID