, 내가왔다 그 (것)들을 일할 수 있습니다.
깔끔하지는 않지만 분명히 작동 할 수 있습니다.
저장 프로 시저를 추가하는 마이그레이션 this 다음과 같습니다
이
using Microsoft.EntityFrameworkCore.Migrations;
using System.Text;
namespace EFGetStarted.AspNetCore.NewDb.Migrations
{
public partial class StoredProcedureTest : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("CREATE PROCEDURE GetBlogForAuthorName");
sb.AppendLine("@authorSearch varchar(100)");
sb.AppendLine("AS");
sb.AppendLine("BEGIN");
sb.AppendLine("-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.");
sb.AppendLine("SET NOCOUNT ON;");
sb.AppendLine("SELECT Distinct Blogs.BlogId, Blogs.Url");
sb.AppendLine("FROM Blogs INNER JOIN");
sb.AppendLine("Posts ON Blogs.BlogId = Posts.BlogId INNER JOIN");
sb.AppendLine("PostsAuthors ON Posts.PostId = PostsAuthors.PostId Inner JOIN");
sb.AppendLine("Authors on PostsAuthors.AuthorId = Authors.AuthorId");
sb.AppendLine("Where Authors.[Name] like '%' + @authorSearch + '%'");
sb.AppendLine("END");
migrationBuilder.Sql(sb.ToString());
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql("DROP PROCEDURE GetBlogForAuthorName");
}
}
}
내가 다음 following 코드로 호출 할 수 있습니다
var blogs = _context.Blogs.FromSql("exec GetBlogForAuthorName @p0", "rod").Distinct();
이후에 관련 데이터의 일부 (한을 받고 시도 많은 관계 데이터 (예 : 게시물 내용)와 블로그는 채워진 게시물 내용이 유출 된 것으로 돌아 왔습니다.
EF 로드맵은 EF 6이 코드 우선의 저장 프로 시저 및 함수를 지원함을 지적합니다. http://entityframework.codeplex.com/wikipage?title=Roadmap – frennky
참고 사항 : http://stackoverflow.com/questions/4873607/how-to-use-dbcontext-database-sqlquerytelementsql-params-with-stored-proced –