Associative table mapping with Entity Framework

2

Abstracting some details I have these classes:

public class Department: Entitie
{
    public string Description { get; private set; }
    public DateTime CreateDate { get; private set; }
    public virtual ICollection<Service> Services {get; set;}
}

public class Service: Entitie
{
    public string Description { get; set; }
    public Guid DepartmentId { get; set; }
    public virtual Department Department { get; set; }
}

public class DepartmentServiceRequest: Entitie
{
    public Guid DepartmentId { get; set; }
    public virtual Department Department { get; set; }

    public virtual ICollection<Service> Services { get; set; }
}

The DepartmentServiceRequest class is just one of my many attempts to make it work.

A Department provides several Services . A department can request one or more services to another department, and this needs to be previously mapped. That is, he has the services he provides and those he can request. The goal is not to register a Service, it is to map what types it can register.

In the bank the result is this:

I do not understand why he's taking Id from DepartmentServiceRequest to Service .

Is it possible via mapping to inform that it should generate this associative table only with the IDs of Department and Service ? This seems logical to me, thinking relationally.

    
asked by anonymous 26.06.2015 / 17:56

1 answer

3

As I mentioned, the association is incorrect. Some missing information in associative table:

public class DepartmentServiceRequest: Entitie
{
    [Key]
    public Guid DepartmentServiceRequestId { get; set }
    [Index("IUQ_DepartmentServiceRequest_DepartmentId_ServiceId", IsUnique = true, Order = 1)]
    public Guid DepartmentId { get; set; }
    [Index("IUQ_DepartmentServiceRequest_DepartmentId_ServiceId", IsUnique = true, Order = 2)]
    public Guid ServiceId { get; set; }

    public virtual Department Department { get; set; }    
    public virtual Service Service { get; set; }
}

The reason he's taking the key to Service is that you indicated it yourself in this statement:

public virtual ICollection<Service> Services { get; set; }

This is incorrect in an associative scope. With this note, you're claiming that Request has multiple services. Therefore, each Service is connected to one, and only one Request , which is wrong.

The Entity Framework automatically tries to fix this for you, and inserting the extra key is a correct behavior. What is not correct is modeling. In the other answer I gave I had already stated that. I'm just typing in other words.

It does not cost to repeat: for the behavior to be complete, also change Department and Service to accept associative link:

public class Department: Entitie
{
    public string Description { get; private set; }
    public DateTime CreateDate { get; private set; }

    public virtual ICollection<Service> Services {get; set;}
    public virtual ICollection<DepartmentServiceRequest> DepartmentServiceRequests {get; set;}
}

public class Service: Entitie
{
    public string Description { get; set; }
    public Guid DepartmentId { get; set; }

    public virtual Department Department { get; set; }
    public virtual ICollection<DepartmentServiceRequest> DepartmentServiceRequests {get; set;}
}

[Index] , introduced in this form from the Entity Framework 6.1.0, guarantees the uniqueness of the associative register. Additional validations may be required in the application to avoid extraneous errors of key duplication for the user.

    
26.06.2015 / 18:17