How to declare an image in the ASP.NET Core Model Class?

Asked

Viewed 133 times

0

I’m following this tutorial from Microsoft:

https://docs.microsoft.com/pt-br/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

Creating a relational data model using Entity Framework 6 but I don’t know how to put a variable in a class that was an image and would serve as a profile photo of the student in this case. I thought I’d declare something like:

{
public class Pessoa
    {
        [Key]
        public Guid Id_Pessoa { get; set; }

        [ForeignKey("Contato")]
        public int Contato_Id { get; set; }
        public virtual ICollection<Contato> ListaContatos { get; set; }
        [Required]
        public string Nome { get; set; }
        [Required]
        public string Sexo { get; set; }
        public string Sexualidade { get; set; }
        [Display(Name = "Apelido")]
        public string NickName { get; set; }
        [Required]
        [Display(Name = "Data de Nascimento")]
        [DataType(DataType.Date)]
        public DateTime DataNascimento { get; set; }
        [Display(Name = "RG")]
        public long Rg { get; set; }
        [Display(Name = "CPF")]
        public long Cpf { get; set; }
        public float Peso { get; set; }
        public float Altura { get; set; }

       //Aqui deve haver uma foto para o perfil da pessoa
        public System.IO.MemoryStream Ft_Avatar { get; set; }
    }
}


1 answer

0


Just create a byte array type property:

public byte[] Imagem { get; set; }

However, this way, every time you upload a Person, the image will be loaded together, leaving the system heavier. It is therefore recommended to create a separate class for the Image:

public class ImagemPessoa
{
    [ForeignKey("Pessoa")]
    public int Pessoa_Id { get; set; }
    public byte[] Imagem { get; set; }
}

And add a virtual property to the person class to access the image:

public virtual ImagemPessoa Avatar { get; set; }

This way, the image will only be loaded if you access the person property. Avatar.

  • Thank you very much.

Browser other questions tagged

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