How to create List of a class inside another class and not be mapped in the Entity Frameworks

0

I have a class called Camera

[Table("Camara")]
public class Camara
{
    [Key]
    public int CamaraId { get; set; }

    [Display(Name = "Nome")]
    [Required(ErrorMessage = "O Campo {0} é requirido!")]
    [StringLength(50, MinimumLength = 2, ErrorMessage = "O campo {0} deve ter no mínimo {2} e no máximo {1} caracteres.")]
    public string Nome { get; set; }
}

I have the class named Side Type

[Table("TipoLado")]
public class TipoLado
{
    [Key]
    public int TipoLadoId { get; set; }

    [Display(Name = "Nome")]
    [Required(ErrorMessage = "O Campo {0} é requirido!")]
    [StringLength(50, MinimumLength = 2, ErrorMessage = "O campo {0} deve ter no mínimo {2} e no máximo {1} caracteres.")]
    public string Nome { get; set; }
}

I want to include this list of Cameras items to be viewed in another class called Sequence if not mapped by Entity Frameworks.

public class Sequencia
    {
        public Sequencia()
        {
            DataAbate = DateTime.Now;            
        }

        [Key]
        public int SequenciaId { get; set; }

        [Display(Name = "Data do Abate")]
        [Required(ErrorMessage = "O Campo {0} é requirido!")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
        [DataType(DataType.Date)]
        public DateTime DataAbate { get; set; }

        [Display(Name = "Lote")]
        [Required(ErrorMessage = "O Campo {0} é requirido!")]
        public int Lote { get; set; }

        [Display(Name = "Numero Sequencia")]
        [Required(ErrorMessage = "O Campo {0} é requirido!")]
        [Range(1, double.MaxValue, ErrorMessage = "Favor Digitar um {0} maior que zero!")]
        public int NumeroSequencia { get; set; }

        [Display(Name = "Lado A")]
        [Required(ErrorMessage = "O Campo {0} é requirido!")]
        [Range(1, double.MaxValue, ErrorMessage = "Selecione o {0} corretamente!")]
        public int LadoA { get; set; }

        [Display(Name = "Câmara Lado A")]
        [Required(ErrorMessage = "O Campo {0} é requirido!")]
        [Range(1, double.MaxValue, ErrorMessage = "Selecione o {0} corretamente!")]
        public int CamaraLadoA { get; set; }

        [Display(Name = "Lado B")]
        [Required(ErrorMessage = "O Campo {0} é requirido!")]
        [Range(1, double.MaxValue, ErrorMessage = "Selecione o {0} corretamente!")]
        public int LadoB{ get; set; }

        [Display(Name = "Câmara Lado B")]
        [Required(ErrorMessage = "O Campo {0} é requirido!")]
        [Range(1, double.MaxValue, ErrorMessage = "Selecione o {0} corretamente!")]
        public int CamaraLadoB { get; set; }

        [NotMapped]
        public List<Camara> CamaraList { get; set; }
    }

Rectifying the question I have two screens One that registers Cameras and the other Type

In the Sequence screen I choose the Camera where Side A or B is and Type that Side A and Side B is

IndexIwantthenamesoftheselecteditemstoappear

    
asked by anonymous 13.03.2018 / 16:01

1 answer

0

The Ideal would be you create a Manager class for Camera, it would be like this.

public class Cameras : List<Camera> {
}

You could also use ICollection<Camara> but I believe you have to implement several methods that already exist in List .

Your Sequencia class would not have the property

public virtual ICollection<Camara> GetCamara { get; set; }

In your controller you would instantiate the class Cameras and use it anywhere to add an anova camera. Ex:

public classe SequenciaController : ApiController {

   public IHttpActionResult Add([FomBody] Sequencia sequencia){
      var cameras = new Cameras();

      cameras.Add(new Camera(){
        Nome = "Camera Teste";
      });
   }
}  

In this example, delete the routes, if so I can put them if you need them.

In case of getting the names, you can create an anonymous object or a DTO , I think it would be your only output. Here's an example. I'm considering that the Sequence table would be its index. And I will consider any context. I am also considering that you have made the EF relationship with the Camera and Sequence entities.

var list = seuContexto.Set<Sequencia>()        
    .Where(s = s.SequenciaId.Equals(id))
    .Select(s => new {
       NomeCameraA = s.CameraLadoA.Name
    });

The list object should be returned by its controller, Select who creates its anonymous object, so you mount an object to your view, any way you need it.

It is extremely important that the relationship between Sequencia and Camera complies with EF rules.

I hope it has helped you, anything I say that I improve.

    
13.03.2018 / 18:16