views:

36

answers:

1

I have a stored procedure in SQL Server that returns some data via a 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

The ConfigurationID column returned by this procedure in some cases returns a NULL.

However, in Visual Studio, when I drag this stored procedure from the Server Explorer into my DBML file that auto generates the code for use with LINQ, it incorrectly sets this column to a non-nullable variable type:

    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 ....

}

I need the definition to be:

private int? _ConfigurationID;

not:

private int _ConfigurationID;

The definition of the method also has an attribute marking it as Int NOT NULL. I don't mind fixing this manually in the generated code, but if I need to make changes to the stored procedure and re-import it, the variables go back to non-nullable, which causes an error, and it's a real pain to keep this updated.

How can I either get it to import correctly and allow nulls, or make sure my edited code doesn't get overwritten? Is there some way to add attributes to the stored procedure in SQL Server to mark the column as nullable?

Thanks, Ben

+1  A: 

I'm worked this problem out myself. I was taking the column from the wrong table in the query. If I change the query to this, it works:

        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
BG100