When the property is not int such as forcing the entity to create a field in the table as nullable: false

0
The migration is generating the name field in the table as nullable: true I do not want it to be true, how do I solve this problem?

 public class Professor
{
    public string Id { get; set; }
    public string Nome { get; set; }
    public string Sobrenome { get; set; }
    public IList<ProfessorTurma> Turmas { get; set; }

    public Professor(string nome, string sobrenome)
    {
        Nome = nome;
        Sobrenome = sobrenome;
        //Turmas = new List<Turma>();
    }

    public bool ValidaProfessor()
    { 
        if(string.IsNullOrEmpty(Nome) || string.IsNullOrEmpty(Sobrenome)
            || Nome.Any(x => char.IsDigit(x)) || Sobrenome.Any(x => char.IsDigit(x))
            )
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}

public partial class TurmaUsuarioProfessorProfessorTurma : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateTable(
            name: "Professores",
            columns: table => new
            {
                Id = table.Column<string>(nullable: false),
                Nome = table.Column<string>(nullable: true),
                Sobrenome = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Professores", x => x.Id);
            });
    
asked by anonymous 31.10.2018 / 19:55

1 answer

1

Since in C # string are always of type by reference and with this they always accept null values because they are nullable , EF creates the fields as if they accept null data by default. >

To place mandatory ownership you can use Data Annotation [Required] on top of your property, eg:

public class Professor
{
    public string Id { get; set; }
    [Required]
    public string Nome { get; set; }
    [Required]
    public string Sobrenome { get; set; }
    public IList<ProfessorTurma> Turmas { get; set; }
}
    
31.10.2018 / 20:16