Reputation: 107
I have 4 tables and between them there is one that is an intermediate table AM_PERFIL_APLICACIONES_TBL
and when the mapping the intermediate table does not appreciate me in the edmx and I have read that several articles in which they explain that it is a matter of relations of many to many.
The question is how could I do a select to the 4 tables with Linq.
How can I UPDATE
, INSERT
or DELETE
the table AM_PERFIL_APLICACIONES_TBL
.
I have something like this
var query_modulo = (from a in menu.AM_USUARIOS_TBL
join b in menu.AM_PERFIL_APLICACIONES_TBL on a.ID_PERFIL equals b.ID_PERFIL
join c in menu.AM_APLICACIONES_TBL on b.ID_APLICACION equals c.ID_APLICACION
join d in menu.AM_MODULOS_TBL on c.ID_MODULO equals d.ID_MODULO
where a.ID_USUARIO == _usrid
select new { d.ID_MODULO, d.DESCRIPCION }).Distinct().ToList();
Upvotes: 1
Views: 320
Reputation: 34783
When you set up your object model if it is a Many-to-Many table between Usuarios (U) and Aplicaniones (A) you would likely have a collection of (A) inside each (U), and a collection of (U) inside of each (A).
However, this is on the assumption that your linking table, Perfil_Aplicaniones (PA) is set up to just contain the ID of (U) (assumed to be ID_Perfil) and the ID of the Aplicanion. (A) (assumed to be ID_Aplicanion)
If this is the case, Entity Framework will handle the intermediary table automatically. To set up a new link between (U) and (A) you add the (A) reference to your u.Aplicaniones collection, and you add the (U) reference to your A.Usarios (or Perfils, whichever it is) When you persist the objects, EF should create the joining record automatically.
If instead though your (U) and (A) entities contain collections of PAs instead, then you will need to create the (PA) entity explicitly, set the references to the (U) and (A) records, then add that (PA) record to the U.Perfil_Aplicaniones and the A.Perfil_Aplicaniones respectively.
It all depends on how your entities (classes) are structured. EF can manage linking tables automatically, but from memory the linking table must contain only the linking ID columns. Otherwise you have to map the linking table manually and manage the associations.
Upvotes: 1