Reputation: 4557
I have a simple three table reference. It looks plain enough. The associations there are created in the DBML. They are not actually foreign keys in the table definition. Why? That's a long story. Trust me when I say, I've fought that battle and lost.
When I attempt to add a new FileType record using LINQ, it crashes with the error: Cannot insert the value NULL into column Id, table FileType.
_fileType = new FileType()
{
Id = def.ID, // This is the integer "1"
Name = def.Name,
FileCategoryId = def.Category.ID,
DateLabel = def.DateLabel,
FileNamePrefix = def.Prefix,
FolderName = def.Folder,
CreateInspection = 0
};
try
{
dc.FileTypes.InsertOnSubmit(_fileType);
dc.SubmitChanges();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
But if you'll notice the code, Id is being set. I've checked the value and it is not null. It is an integer. What am I missing or doing wrong??
I did check to make sure that the FileCategory record of the correct ID exists. Is there anything else I'm missing?
Upvotes: 4
Views: 426
Reputation: 4557
In your table mapping, is the field marked as 'Auto Generated Value' in the Properties pane? I've had insert/update issues with that before... – Tieson T. Mar 7 at 6:52
I would give him the props for this, but he left this as a comment, and not an official answer.
Upvotes: 0
Reputation: 117310
I have seen something like this before.
It happens when you only assign the 'id' and not the related object.
Try the following:
new FileType
{
FileCategory = def.Category,
...
}
Upvotes: 2