代码之家  ›  专栏  ›  技术社区  ›  BG100

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

  •  0
  • BG100  · 技术社区  · 14 年前

    我在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文件中时,它会错误地将此列设置为不可为null的变量类型:

        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。我不介意在生成的代码中手动修复这个问题,但是如果我需要对存储过程进行更改并重新导入它,那么变量会返回到不可为null的状态,这会导致错误,并且保持更新是一件非常痛苦的事情。

    如何才能正确导入并允许空值,或者确保编辑的代码不会被覆盖?是否有某种方法可以向SQLServer中的存储过程添加属性以将列标记为可空?

    谢谢, 本

    1 回复  |  直到 11 年前
        1
  •  1
  •   BG100    14 年前

            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