Đối với NET Core (EntityFrameworkCore), tôi đã được có thể khiến họ làm việc.
Có thể không phải là gọn gàng nhất, nhưng điều này chắc chắn hoạt động.
Việc chuyển để thêm các thủ tục lưu trữ trông giống như 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");
}
}
}
Sau đó tôi có thể gọi nó với mã following:
var blogs = _context.Blogs.FromSql("exec GetBlogForAuthorName @p0", "rod").Distinct();
Sau đó cố gắng nhận được một số dữ liệu liên quan (một đến nhiều dữ liệu quan hệ, ví dụ: Nội dung bài đăng) và blog đã trở lại với nội dung Đăng đầy như đã được xuất bản.
Điểm lộ trình EF mà EF 6 sẽ hỗ trợ các thủ tục và chức năng được lưu trữ cho Mã đầu tiên. http://entityframework.codeplex.com/wikipage?title=Roadmap – frennky
Xem thêm: http://stackoverflow.com/questions/4873607/how-to-use-dbcontext-database-sqlquerytelementsql-params-with-stored-proced –