Set virtual field name

Asked

Viewed 77 times

1

I have the following code in my model SubGrupo:

namespace Aplicacao.Core.Dominio
{
    public class SubGrupo
    {
        [Key]
        public int Codigo { get; set; }

        [Required(ErrorMessage = "Campo Obrigatório.")]
        public string Descricao { get; set; }

        public virtual TipoFormulario TipoFormulario { get; set; }
    }
}

Model TipoFormulario

namespace Aplicacao.Core.Dominio
{
    public class TipoFormulario
    {
        [Key]
        public int Codigo { get; set; }

        [Required(ErrorMessage = "Campo Obrigatório.")]
        public string Descricao { get; set; }
    }
}

In this case, the Keyof the model TipoFormulario is Codigo. So in the bank is generated TipoFormulario_Codigo

How can I define the name that will be generated ? I would like to name the column to TipoFormulario

1 answer

2


Thus:

namespace Aplicacao.Core.Dominio
{
    public class SubGrupo
    {
        [Key]
        public int Codigo { get; set; }
        [Column("TipoFormulario")]
        [ForeignKey("TipoFormulario")]
        public int TipoFormularioId { get; set; }

        [Required(ErrorMessage = "Campo Obrigatório.")]
        public string Descricao { get; set; }

        public virtual TipoFormulario TipoFormulario { get; set; }
    }
}
  • But so instead of naming the existing one, I’ll be creating a new field, not?

  • Not necessarily. The field will exist anyway. You’re just telling the Entity Framework what the foreign key looks like.

  • Copying and placing the excerpt as you typed, generated me the column "Typoformulario" and the column "Typoformulario_codigo"

  • I think you need to force the [ForeignKey]. Look now.

  • Fucked up, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.