È possibile utilizzare Inner Join
nel metodo FromSql
del core Entity Framework?
Ho usato, ma ho ricevuto questo errore:
SqlException: la colonna "Id" è stata specificata più volte per "c".
Questo è il mio codice:
return DbContext.Contacts
.FromSql<Contact>("Select * From Contacts Inner Join Phones on Phones.ContactId = Contacts.Id Where Contacts.Id <= 2 And Phones.PhoneNumber='01234567890'")
.Include(o => o.RegisteredByUser)
.AsNoTracking()
.ToListAsync();
La colonna Id
viene visualizzata in entrambe le tabelle, Phones
e Contacts
. Invece di inserire *
, è meglio scegliere i campi richiesti nella query come segue.
Si prega di notare, è necessario specificare quale ID si desidera, Se si desidera entrambi, in tal caso è possibile utilizzare nomi di alias come segue.
Select Phones.Id as PhoneId, Contacts.Id as ContactsId
, ....
La tua query finale dovrebbe assomigliare alla seguente query.
Select Contacts.Id, ... From Contacts Inner Join Phones on Phones.ContactId = Contacts.Id Where Contacts.Id <= 2 And Phones.PhoneNumber='01234567890