EF Core always include ALL the columns of the class in the INSERT statement. How can I modify the script so that the INSERT statement will only include values for which I have provided a value?
Let's say I have a User class with 3 properties:
public int Id { get; set; } // this one is linked to a auto increment ID column
public string Name { get; set; }
public int Amount { get; set; } // linked to a column that cannot be null and has a default value configured in SQL Server
So what I am doing to insert a new record into the User table is:
User newUser = new User();
newUser.Name = "John";
await _dbc.AddAsync(newUser);
await _dbc.SaveChangesAsync();
The INSERT statement will include Name = "JOHN", BUT will also include Amount = null, so the query will fail because null is not accepted for Amount. I want to remove the Amount column from the generated INSERT statement to simply send
INSERT INTO User (NAME)
VALUES ('John')
and I want to let the SQL Server insert the default configured value into the Amount column.
It is a simple example, my real situation have many columns for which I have default value for insertion, and I do not want the INSERT statement that is generated to include ALL the columns from the User class, only those for which I provide a value.