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

Asked

Viewed 49 times

0

I have a class called Chamber

[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 called Tipo Lado

[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 the 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

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

Tela de cadastro de sequencia

Index I want the names of the selected items to appear

Tela Index do cadastro de sequencias

  • Why do you need the EPH No Mother? I ask this because maybe what you want to do, is not to be done in the entity but in any service.

  • @Rodrigok. B as you said I want to do any job, is that I want Sequencia to include the Camera table in Sequenciacontroller. would pass this scheme ?

  • Does Sequencia have any connection with camera? I’ll give you an answer about what I think you should do for what I understand of your problem. Anything you can say will make me feel better.

  • @Rodrigok. B is the following Sequence has connection with Camera yes, the problem is that in Sequence I have two fields (Side A) and (Side B) that are each, a Dropdownlist that shows the name of the Camera and that takes Camera their respective ID and now I want in Index and Detail show instead of the ID show the names of the Cameras, understood ?

  • Get it, see my answer if it helps you, and comment on me to improve for you, I see that maybe you need a third class yet.

  • @Rodrigok. B you could send me a contact by email to be able to exemplify this question better, if not give comment yes or no blz ? Negative I delete this message blz

  • include the details in your question

  • @Rodrigok. B corrected the question, now I am explaining better my need.

Show 4 more comments

1 answer

0

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

public class Cameras : List<Camera> {
}

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

Your class Sequenciawould not have the property

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

In your controller you would instantiate the class Cameras and would 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 applicable I can put if you need.

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

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

the list object must be returned by your controller, O Select who creates your anonymous object, so you mount an object apra your view, the way you need it.

It is extremely important that the relationship between Sequencia and Camera are in accordance with the rules of the RU.

I hope I’ve helped you, anything you say that’s better.

Browser other questions tagged

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