Jou*_*and 5 entity-framework entity-framework-core asp.net-core
是否可以Inner Join在FromSql实体框架核心的方法中使用?
我用过,但出现此错误:
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();
Run Code Online (Sandbox Code Playgroud)
您的Id列同时出现在表Phones和中Contacts。*您最好选择查询中所需的字段,而不是放置 ,如下所示。
请注意,您需要指定您想要的 Id,如果您想要两者,在这种情况下您可以使用如下所示的别名。
Select Phones.Id as PhoneId, Contacts.Id as ContactsId, ....
您的最终查询应类似于以下查询。
Select Contacts.Id, ... From Contacts Inner Join Phones on Phones.ContactId = Contacts.Id Where Contacts.Id <= 2 And Phones.PhoneNumber='01234567890
Run Code Online (Sandbox Code Playgroud)