I\'m using the following query:
INSERT INTO role (name, created) VALUES (\'Content Coordinator\', GETDATE()), (\'Content Viewer\', GETDATE())
In my case,
I was trying to update my model by making a foreign key required, but the database had "null" data in it already in some columns from previously entered data. So every time i run update-database...i got the error.
I SOLVED it by manually deleting from the database all rows that had null in the column i was making required.
As id is PK it MUST be unique and not null. If you do not mention any field in the fields list for insert it'll be supposed to be null or default value. Set identity (i.e. autoincrement) for this field if you do not want to set it manualy every time.
I'm assuming that id
is supposed to be an incrementing value.
You need to set this, or else if you have a non-nullable column, with no default value, if you provide no value it will error.
To set up auto-increment in SQL Server Management Studio:
Design
Column Properties
Indentity Specification
, set (Is Identity)=Yes
and Indentity Increment=1
You either need to specify an ID in the insert, or you need to configure the id column in the database to have Identity Specification = Yes.
You can insert a value manually in the ID column (here I call it "PK"):
insert into table1 (PK, var1, var2)
values ((select max(PK)+1 from table1), 123, 456)
if you can't or don't want to set the autoincrement property of the id, you can set value for the id for each row, like this:
INSERT INTO role (id, name, created)
SELECT
(select max(id) from role) + ROW_NUMBER() OVER (ORDER BY name)
, name
, created
FROM (
VALUES
('Content Coordinator', GETDATE())
, ('Content Viewer', GETDATE())
) AS x(name, created)