Complex mapping Entity Framework

Asked

Viewed 176 times

3

I am creating a class structure that follows the following form:

Department -Service

A department provides various types of service. A specific service can only be provided by one department, not by many.

A department can request services from others, and this is previously mapped: Department To may request the service B (B who is formerly in Department C).

My problem starts there, because I have to say that a Department may require one or more other services. I tried a few ways, including putting two attributes of the type "Service" in class Department (two collections), but when the Entity generates the database the table structure becomes a zone, with the table of Services with two fields relating to Department.

Thinking relationally, I would have a third table with Department and Service id forming a composite key.

How I could map to classes (without generating a zone in the bank)?

Thank you!

1 answer

2


The department providing a service is one thing. A department taking a service is another. You are treating two different things as the same thing. By your explanation, what we have is:

public class Departamento 
{
    [Key]
    public int DepartamentoId { get; set; }

    ...
    public virtual ICollection<Servico> Servicos { get; set; }
}

public class Servico 
{
    [Key]
    public int ServicoId { get; set; }
    public int DepartamentoId { get; set; }

    ...
    public virtual Departamento Departamento { get; set; }
}

If one department is requesting another’s service, I believe there must be one Model separately for this. For example:

public class RequisicaoServico
{
    [Key]
    public int RequisicaoServicoId { get; set; }
    [Index("IUQ_RequisicaoServico_ServicoId_DepartamentoId", IsUnique = true, Order = 1)]
    public int ServicoId { get; set; }
    [Index("IUQ_RequisicaoServico_ServicoId_DepartamentoId", IsUnique = true, Order = 2)]
    public int DepartamentoId { get; set; }

    [Required]
    public DateTime DataRequisicao { get; set; }
    [Required]
    public Boolean Finalizado { get; set; }
    [DataType(DataType.MultilineText)]
    [Required]
    public String Descricao { get; set; }

    public virtual Departamento Departamento { get; set; }
    public virtual Servico Servico { get; set; }
}

[Index], introduced in this form from the Entity Framework 6.1.0, ensures the uniqueness of the associative record. Additional validations may be required in the application to avoid strange key duplicity errors for the user.

Being the associative table, the only thing you would need to do now is to update the Models of Departamento and Servico to admit that they are related to a RequisicaoServico, that is to say:

public class Departamento 
{
    [Key]
    public int DepartamentoId { get; set; }

    ...
    public virtual ICollection<Servico> Servicos { get; set; }
    public virtual ICollection<RequisicaoServico> RequisicoesServicos { get; set; }
}

public class Servico 
{
    [Key]
    public int ServicoId { get; set; }
    public int DepartamentoId { get; set; }

    ...
    public virtual Departamento Departamento { get; set; }
    public virtual ICollection<RequisicaoServico> RequisicoesServicos { get; set; }
}
  • So, almost that. I’m not actually opening a service. I’m just mapping which department can open which services from other departments. The Marketing Department can request the Contract Verification service (which in turn is the domain of the Legal department). Got it?

  • @Stefanosandes So, just change the association to reflect this reality. With some checks of uniqueness on Controller, a logic that does not allow the same association to be entered twice in the database is guaranteed.

  • Right. I think my problem is mapping. The Department and Service tables are beans and rice. Department has name and a list of services and Service has a Department and a name. So far everything works fine. When I create the Departmental Service class it has a Department and various Services. A department is entitled to request a list of services. When I am the bank Entity puts the Department ID in the Service table, and for me that doesn’t make sense. It’s like putting the person’s ID on the car just because a car can carry people.

  • So your mapping is wrong. I think it would be the case to open another question with the code Models.

  • You’re right, buddy, I’ll do that. Thank you very much!

Browser other questions tagged

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