Execute non-query procedure not working asp.net core

被刻印的时光 ゝ 提交于 2019-12-06 15:53:30

问题


I want to execute a stored procedure which returns three values (Email, Name, CompanyID) and get one parameter (CompanyID) but it's not working.

I have created a class with these properties and a stored procedure which returns the data. By it is showing DatabaseFacade error.

My code is:

 List<MyClass> AppUser = new List<MyClass>(); //Class with three properties
 SqlParameter param1 = new SqlParameter("@CompanyID", CompanyID);
 AppUser = _context.Database.SqlQuery<CC>("GetUserAndRolesForCompany", param1).ToList(); 

Showing this error: I have include System.Linq

'DatabaseFacade' does not contain a definition for 'SqlQuery' and no extension method 'SqlQuery' accepting a first argument of type 'DatabaseFacade' could be found (are you missing a using directive or an assembly reference?)


回答1:


Take help from this link https://docs.microsoft.com/en-us/ef/core/querying/raw-sql

In Core 2.0 or EntityFramework 7 does not support SqlQuery feature which was available in previous version of ef 6.

Below is the example how you can execute sp in EntityFramework 7.

 public List<UserDetails> GetUserDetailsUsingSP(int LoggedInID)
        {
            var loggedInUser = new SqlParameter("Id", LoggedInID);

            return WidServices
                .FromSql("EXECUTE WID_Services_GetAll  @Id ", loggedInUser)
                .FirstOrDefault();
        }

Note : Add this method where you are adding your DbSet under your main db context class and then call this method by instantiating your dbContext.



来源:https://stackoverflow.com/questions/40428158/execute-non-query-procedure-not-working-asp-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!