views:

894

answers:

1

We moved the .dbml file out of the website and into another assembly so it could be shared across multiple applications. The problem is that now I'm getting an error that doesn't make any sense.

The basic query is like this:

from D in Devices
select new
{
    EquipmentTypeID = D.DMXDeviceModel.DMXDeviceClass.DMXEquipmentType.ID,
    //... more fields
}

There are several fields queried like that, and now they are all throwing the following exception: The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type.

The ID field(s) in question are not nullable in the database, or in the Linq entity class. Everything matches up correctly, so there shouldn't even be a null value anywhere in there. The code also worked perfectly before "moving" the .dbml file to another assembly.

Actually, we didn't move it because there were some other errors and problems when we tried. I recreated it in the new assembly, but that shouldn't matter because it was generated from the same database.

I've also tried selecting anything where D.DMXDeviceModel.DMXDeviceClass.DMXEquipmentType.ID is null, and no results were returned.

EDIT: by request here is the xml for the dbml file. Or at least the relevant portions:

<Table Name="dbo.DMXDevice" Member="DMXDevices">
  <Type Name="DMXDevice">
    <Column Name="ID" Type="System.Int32" DbType="Int NOT NULL IDENTITY" IsPrimaryKey="true" IsDbGenerated="true" CanBeNull="false" />
    <Column Name="DeviceModelID" Type="System.Int32" DbType="Int" CanBeNull="true" />    
    <Association Name="DMXDeviceModel_DMXDevice" Member="DMXDeviceModel" ThisKey="DeviceModelID" OtherKey="ID" Type="DMXDeviceModel" IsForeignKey="true" />    
  </Type>
</Table>

  <Table Name="dbo.DMXDeviceModel" Member="DMXDeviceModels">
    <Type Name="DMXDeviceModel">
      <Column Name="ID" Type="System.Int32" DbType="Int NOT NULL IDENTITY" IsPrimaryKey="true" IsDbGenerated="true" CanBeNull="false" />
      <Column Name="DeviceClassID" Type="System.Int32" DbType="Int NOT NULL" CanBeNull="false" />
      <Column Name="Model" Type="System.String" DbType="NVarChar(255) NOT NULL" CanBeNull="false" />      
    </Type>
  </Table>

It turns out anything referenced through DMXDeviceModel throws this exception when DeviceModelID is null. It seems to be a problem creating the anonymous type to handle a null DeviceModelID... but why wasn't it happening before?

EDIT EDIT: You know, I also moved to .NET 3.5 SP1 and that changed some of the other behavior on the site. Maybe that's the real culprit here.

+2  A: 

The answer is to cast the expression to a nullable type, which lets the anonymous type know to expect it. I suspect this was a change with SP1.

EquipmentTypeID = ((int?)D.DMXDeviceModel.DMXDeviceClass.DMXEquipmentType.ID),
Telos
Also, found that "as" doesn't work: "EquipmentTypeID = D.DMXDeviceModel.DMXDeviceClass.DMXEquipmentType.ID as int?". You must use the cast operator as Telos says.
Joseph Kingry