I have the following Entity model:
public class Todo
{
[Required]
public int ID { get; set; }
public int OrderId { get; set; } //Not required
public string Description { get; set; }
public bool Finished { get; set; }
public DateTime CreationDate { get; set; }
public int Priority { get; set; } //Not required
public string CreatedBy { get; set; }
public bool Deleted { get; set; }
}
In the corresponding database table all of the fields are created as “not null”. I want to allow some of the fields to be null. How do I do this?
On the database side, you will have to change the fields you want to be optional so that they can be null. The ALTER TABLE statement will do the trick.
On the application side, you need to use nullable types. Try this:
A nullable type is a variation of a regular value type with the difference that it can be null. In your code you can test for null with the
HasValueproperty:All operators on that type are lifted, meaning that you can still do arithmetic with them: