Entity framework 在FromSql实体框架核心方法中使用内部联接

Entity framework 在FromSql实体框架核心方法中使用内部联接,entity-framework,asp.net-core,entity-framework-core,Entity Framework,Asp.net Core,Entity Framework Core,是否可以在实体框架核心的FromSql方法中使用internaljoin 我用过,但我得到了这个错误: SqlException:为“c”多次指定了列“Id” 这是我的代码: return DbContext.Contacts .FromSql<Contact>("Select * From Contacts Inner Join Phones on Phones.ContactId = Contacts.Id Where Contacts.Id <= 2 And Ph

是否可以在实体框架核心的
FromSql
方法中使用
internaljoin

我用过,但我得到了这个错误:

SqlException:为“c”多次指定了列“Id”

这是我的代码:

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();
返回DbContext.Contacts

.FromSql(“Select*From Contacts internal Join Phones on Phones.ContactId=Contacts.Id Where Contacts.Id您的
Id
列将出现在两个表中,
Phones
Contacts
。您最好选择查询中所需的字段,而不是放置
*

请注意,您需要指定您想要的Id,如果您想要两者,在这种情况下,您可以使用如下别名

选择Phones.Id作为PhoneId,选择Contacts.Id作为ContactsId

您的最终查询应该类似于下面的查询

Select Contacts.Id, ... From Contacts Inner Join Phones on Phones.ContactId = Contacts.Id Where Contacts.Id <= 2 And Phones.PhoneNumber='01234567890
选择Contacts.Id,…From Contacts internal Join Phones on Phones.ContactId=Contacts.Id Where Contacts.Id