问题
I used Asp.Net Core version 2 and code-first. I tried to use .FromSql
to call a stored procedure. I did the same as Microsoft said:
var blogs = context.Blogs
.FromSql($"SELECT * FROM dbo.SearchBlogs({searchTerm})")
.Include(b => b.Posts)
.ToList();
and my stored procedure only contains the line of code
ALTER PROCEDURE [dbo].[GetCouffierSearch]
AS
BEGIN
SET NOCOUNT ON;
SELECT *
FROM AspNetUsers
END
My code in the API:
public IQueryable<ApplicationUser> SelectNearByUser()
{
var query = "execute dbo.GetCouffierSearch";
var res = _context.Users.FromSql(query);
return res.Include(x => x.CoiffeurServiceLevels);
}
and my class ApplicationUser
contains definition for CoiffeurServiceLevels
with type of ICollection
:
public class ApplicationUser: IdentityUser
{
public ApplicationUser()
{
//CoiffeurServiceLevels = new Collection<CoiffeurServiceLevel>();
//Requests = new Collection<Request>();
//Tickets = new Collection<Ticket>();
//UserRatings = new Collection<Rating>();
//CouffierRatings = new Collection<Rating>();
}
public string PhotoUrl { get; set; }
public string Lat { get; set; }
public Language Language { get; set; }
public string Address { get; set; }
public string FullName { get; set; }
public string Lng { get; set; }
public string PersonalId { get; set; }
public Place Places { get; set; }
public long? PlaceId { get; set; }
public int? CouffierNumber { get; set; } = 1;
public long CityId { get; set; }
public City City { get; set; }
public bool IsSuspended { get; set; }
public bool IsOnline { get; set; }
public string ConfirmedToken { get; set; }
public string ResetPasswordlToken { get; set; }
public DateTime BirthDate { get; set; }
public bool Gender { get; set; }
//#region RelationsClass
public virtual ICollection<CoiffeurServiceLevel> CoiffeurServiceLevels { get; set; }
// public ICollection<Request> Requests { get; set; }
////public ICollection<Request> RequestsCouffier { get; set; }
//public virtual ICollection<Ticket> Tickets { get; set; }
public virtual ICollection<Rating> UserRatings { get; set; }
//public virtual ICollection<Rating> UserRatingsBy { get; set; }
//public ICollection<IdentityRole> Roles { get; set; }
//Roles = new Collection<IdentityUserRole>();
//#endregion
}
When I call it, I get this error:
Message: The Include operation is not supported when calling a stored procedure.
Source: Microsoft.EntityFrameworkCore.Relational
回答1:
You can explicitly load a navigation property via the DbContext.Entry(...) API. https://docs.microsoft.com/en-us/ef/core/querying/related-data#explicit-loading
public IQueryable<ApplicationUser> SelectNearByUser()
{
var query = "execute dbo.GetCouffierSearch";
var res = _context.Users.FromSql(query);
_context.Entry(res)
.Collection(x => x.CoiffeurServiceLevels)
.Load();
return res;
}
回答2:
I had the same issue and have found that if you load both entities separately they auto-link together as long as the data model supports this.
I have customer entities loaded via a stored procedure with a showroom ID and I wanted to display the name of the showroom from the showroom table:
First I populate the showrooms:
Showroom = await _context.Showroom.ToListAsync();
Then I call my stored procedure for the customer object:
Customer = await _context.Customer
.FromSql("EXEC sp_GetCustomerList @SearchString, @SortString", searchParam, sortParam)
.ToListAsync();
Lastly I can now access the values from the showrooms list using this syntax:
@Html.DisplayFor(modelItem => item.Showroom.ShowroomName)
I am not sure if there is much of a performance impact doing it this way as you are essentially executing two queries rather than one.
回答3:
I Used Ado.net And Call Procedure
DataTable dt = new DataTable();
using (SqlConnection sqlConn = new SqlConnection(_context.Database.GetDbConnection().ConnectionString))
{
string sql = "<ProcedureName>";
using (SqlCommand sqlCmd = new SqlCommand(sql, sqlConn))
{
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.AddWithValue("@ServiceId", string.Join(',', servicesIds.ToArray()));
sqlCmd.Parameters.AddWithValue("@Page", page);
sqlCmd.Parameters.AddWithValue("@PageSize", pageSize);
sqlConn.Open();
using (SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCmd))
{
sqlAdapter.Fill(dt);
}
}
}
来源:https://stackoverflow.com/questions/47122425/error-when-using-fromsql-and-include-the-include-operation-is-not-supported-wh