-1

I am stuck with this error

Cannot insert the value NULL into column 'FuncionarioId', table 'aspnet-vet_plus-7C2DFFB7-3F35-42B0-88A6-094E42F60676.dbo.Endereco'; column does not allow nulls. UPDATE fails.
The statement has been terminated.

When I run the Update-Database to update my columns.

This is my C# code in the Endereco - model

        [Column("ClienteId")]
        [Display(Name = "Cliente")]
        [DisplayFormat(DataFormatString = "{0:N}", ApplyFormatInEditMode = true)]
        public int ClienteId { get; set; }

        [Column("FuncionarioId")]
        [Display(Name = "Funcionário")]
        [DisplayFormat(DataFormatString = "{0:N}", ApplyFormatInEditMode = true)]
        public int FuncionarioId { get; set; }

        public virtual Cliente Cliente { get; set; }
        public virtual Funcionario Funcionario { get; set; }

I am trying to connect my Enderoco database with the Cliente and Funcionario tables, the Enderoco table has to receive either a ClienteId or an FuncionarioId, it doesn't matter which one, but it must have one of the two.

Does anyone know about this error and how to fix it? Please, let me know!

Bumboobee
  • 31
  • 1
  • 11
  • none of the alternatives worked... what I want to do is, the Endereco table has to receive either a ClienteId or an FuncionarioId, it doesn't matter which one, but it must have one of the two. – Bumboobee Apr 24 '22 at 19:09
  • can you post your migration UP method for add these columns? – sa-es-ir Apr 24 '22 at 19:37
  • SUGGESTION: Make FuncionaroId and ClientId both nullable: 1) `public int? FuncionarioId { get; set; }`, 2) `public int? ClienteId { get; set; }` , then 3) validate that one or the other is non-null in whatever C# code you're using to "insert" or "update". See this thread for more details: [Entity Framework - Cannot insert the value NULL into column?](https://stackoverflow.com/questions/48830041/entity-framework-cannot-insert-the-value-null-into-column) – paulsm4 Apr 24 '22 at 22:26

1 Answers1

0

You can add [Required] attribute on FuncionarioId. I think this can help you.