Question

I'm mapping my database into base object with Linq to SQL. I drag all the tables into my dbml and they all setup nicely. I save the dbml and it creates the classes that represent the tables, etc.

In my database I have a table as such:

CREATE TABLE [dbo].[BidNames](
    [BidNameID] [int] IDENTITY(1,1) NOT NULL,
    [CustomerID] [int] NOT NULL,
    [BidName] [varchar](75) NOT NULL,
 CONSTRAINT [PK_BidNames] PRIMARY KEY CLUSTERED 
(
    [BidNameID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

The BidNameID field is clearly a primary key and obviously NOT NULL. Linq, however defines BidNameID like this:

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_BidNameID", DbType="Int")]
    public System.Nullable<int> BidNameID
    {
        get
        {
            return this._BidNameID;
        }
        set
        {
            if ((this._BidNameID != value))
            {
                if (this._BidName1.HasLoadedOrAssignedValue)
                {
                    throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                }
                this.OnBidNameIDChanging(value);
                this.SendPropertyChanging();
                this._BidNameID = value;
                this.SendPropertyChanged("BidNameID");
                this.OnBidNameIDChanged();
            }
        }
    }

Where BidNameID is defined as System.Nullable. All the other tables in my database resolve correctly so I'm left wondering why this is happening. Any ideas?

Thanks in advance.

EDIT

I have discovered that this problem is related to another table. If I drop the Bids table from my dbml and re-save, the BidNameID column correctly resolves to int. If I put the Bids table back and save it goes back to Nullable. The Bids table has a foreign key into the BidNames table but the data is clean. Here is the structure of the Bids table:

CREATE TABLE [dbo].[Bids](
    [BidID] [int] IDENTITY(1,1) NOT NULL,
    [CustomerID] [int] NOT NULL,
    [ItemID] [int] NOT NULL,
    [Amount] [money] NOT NULL,
    [BidName] [varchar](75) NOT NULL,
    [BidTime] [datetime] NOT NULL,
    [BidNameID] [int] NULL,
 CONSTRAINT [PK_Bids] PRIMARY KEY CLUSTERED 
(
    [BidID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

ALTER TABLE [dbo].[Bids]  WITH CHECK ADD  CONSTRAINT [FK_Bids_BidNames] FOREIGN KEY([BidNameID])
REFERENCES [dbo].[BidNames] ([BidNameID])
GO

ALTER TABLE [dbo].[Bids] CHECK CONSTRAINT [FK_Bids_BidNames]
GO

ALTER TABLE [dbo].[Bids]  WITH CHECK ADD  CONSTRAINT [FK_Bids_Customers] FOREIGN KEY([CustomerID])
REFERENCES [dbo].[Customers] ([CustomerID])
GO

ALTER TABLE [dbo].[Bids] CHECK CONSTRAINT [FK_Bids_Customers]
GO

ALTER TABLE [dbo].[Bids]  WITH CHECK ADD  CONSTRAINT [FK_Bids_Items] FOREIGN KEY([ItemID])
REFERENCES [dbo].[Items] ([ItemID])
GO

ALTER TABLE [dbo].[Bids] CHECK CONSTRAINT [FK_Bids_Items]
GO
Was it helpful?

Solution 2

I don't necessarily understand why but when I alter the Bids table and make the BidNameID field NOT NULL, it fixes the problem with the BidName.BidNameID field. It now resolves to int rather than nullable.

OTHER TIPS

I took your table script, ran in my DB and dragged it into a dbml and it didn't generate as nullable (VS2010, VB.NET 4.0):

<Global.System.Data.Linq.Mapping.ColumnAttribute(
    Storage:="_BidNameID", AutoSync:=AutoSync.OnInsert,
    DbType:="Int NOT NULL IDENTITY", IsPrimaryKey:=True,
    IsDbGenerated:=True)>
    Public Property BidNameID() As Integer
        Get
            Return Me._BidNameID
        End Get
        Set(value As Integer)
            If ((Me._BidNameID = value) _
               = False) Then
                Me.OnBidNameIDChanging(value)
                Me.SendPropertyChanging()
                Me._BidNameID = value
                Me.SendPropertyChanged("BidNameID")
                Me.OnBidNameIDChanged()
            End If
        End Set
    End Property

Maybe try one or more of these?

  1. drop/recreate the physical table
  2. delete/recreate your DB connection in Server Explorer
  3. delete/recreate the dbml
  4. manually edit the designer file
  5. create additional datacontext partial class and manually maintain that table/mapping
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top