Executesql function give error on ef core entity framework cannot convert from 'string' to 'System.FormattableString'

Ahmed Salah Abed Elaziz 390 Reputation points
2023-03-14T09:21:00.2633333+00:00

I work on .net core 7 entity framework core . I get error when run my application on this function

error say cannot convert from 'string' to 'System.FormattableString'

public List<ApplicationDto> ListOfApplications(ApplicationsFilterDto emp)
        {
            var appList = _context.Database.ExecuteSql($"select ApplicationsData.ApplicationID as ApplicationId,Application_Name as ApplicationName,CommonName,d.DetailsName as TypeOfApplication,AccessType,d.DetailsName Criticality,o.OwnerName as ApplicationOwner,DRRequired,se.[DB_Name] as [DataBase] from ApplicationsData left join [dbo].[Details] d with(nolock) on d.ID=ApplicationsData.ApplicationType and d.HeaderId=6 left join [dbo].[Details] d2 with(nolock) on d2.ID=ApplicationsData.Criticality and d2.HeaderId=7\r\nleft join dbo.[Owner] o with(nolock) on o.ApplicationId=ApplicationsData.ApplicationID\r\nleft join dbo.[DataBase] se with(nolock) on se.ServerID=ApplicationsData.ServerId where ApplicationsData.ServerId=" + emp.serverID + " OR SE.[DBID]=" + emp.databaseId + " OR o.OwnerId=" + emp.ownerId + "");
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
            return appList;  
       }

return model as below :

public class ApplicationDto
    {
        public int ApplicationId { get; set; }
        public string ApplicationName { get; set; }
        public string CommonName { get; set; }
        public string TypeOfApplication { get; set; }
        public string AccessType { get; set; }
        public string Criticality { get; set; }
        public string DataBase { get; set; }
        public string BSSVServer { get; set; }
        public string DRRequired { get; set; }
        public string ApplicationOwner { get; set; }


    }

filter model I use it

public class ApplicationsFilterDto
    {
        public int serverID { get; set; }
        public int ownerId { get; set; }
        public int databaseId { get; set; }
    }
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,643 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,397 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,346 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,641 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 114.7K Reputation points
    2023-03-14T09:39:19.1966667+00:00

    Try modifying the where part:

    var appList = _context.Database.ExecuteSql($"select . . . where ApplicationsData.ServerId={emp.serverID} OR SE.[DBID]={emp.databaseId} OR o.OwnerId={emp.ownerId}");
    

0 additional answers

Sort by: Most helpful