Reputation: 635
Good day. I have MSSQL2008 database. And I made a proper model for one of the existing table (VLANs).
public DbSet<VLAN> VLANs { get; set; }
VLAN Model has field:
[Key][Required]
public int VLAN_ID { get; set; } //in the "VLAN" table it is PK
...
Then I try to insert a new value
_db.VLANs.Add(VLANToEdit); //<-- Here VLANToEdit has VLANToEdit.VLAN_ID == 144. No such data in the table
_db.SaveChanges(); //<--Here I got an exception.
MSSQL can't insert a NULL value into VLAN_ID field. But why? I pass a real value but null? What s wrong? I get following message:
"Cannot insert the value NULL into column 'VLAN_ID', table 'VMMExt.dbo.VLANs'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated." string
If i remove [Key] attribute I got an error in other places about missing key fields in VLANs tables.
How to fix this and force to insert with provided primary key value?
Upvotes: 0
Views: 411
Reputation: 635
Thanks deep search on this site! I found it here: C# .NET 4.0 MVC Inserting a record with primary key
All is need was to add an attribute into model: [DatabaseGenerated(DatabaseGeneratedOption.None)]
You may delete my question. Thanks!
Upvotes: 1