C # Why is Linq broken into Null if the value is not null?

I have a simple link to three tables. It looks clear enough. Associations are created in DBML. They are not foreign keys in the table definition. What for? It's a long story. Believe me, when I say: I fought in this battle and lost.

enter image description here

When I try to add a new FileType entry using LINQ, it crashes with an error: Unable to insert a NULL value in the column identifier, the FileType table.

_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 notice the code, Id will be installed. I checked the value and it is not equal to zero. This is an integer. What am I missing or is something wrong?

I checked to make sure that the FileCategory entry of the correct identifier exists. Is there anything else I am missing?

+4
source share
2 answers

In your table mapping, a field labeled β€œAuto Generated Value” in the property pane? I had problems inserting / updating to ... - Tieson T. Mar 7 at 6:52

I would give him the props for this, but he left it as a comment, not an official answer.

0
source

I have seen something like this before.

This happens when you assign only the "id" and not the associated object.

Try the following:

 new FileType { FileCategory = def.Category, ... } 
+2
source

Source: https://habr.com/ru/post/1400157/


All Articles