免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

EntityFrameworkCoreRawSQL

EntityFramework Core Raw SQL

基礎查詢(執行SQL和存儲過程)

啥也不說了,拿起鍵盤就是干,如下:

專注于為中小企業提供成都網站設計、網站制作、外貿營銷網站建設服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業吳興免費做網站提供優質的服務。我們立足成都,凝聚了一批互聯網行業人才,有力地推動了千余家企業的穩健成長,幫助中小企業通過網站建設實現規模擴充和轉變。

 EntityFramework Core Raw SQL

    public class HomeController : Controller
    {        private IBlogRepository _blogRepository;        public HomeController(IBlogRepository blogRepository)
        {
            _blogRepository = blogRepository;
        }        public IActionResult Index()
        {            var list = _blogRepository.GetList();            return Ok();
        }
    }

 EntityFramework Core Raw SQL

 EntityFramework Core Raw SQL

    public class BlogRepository : EntityBaseRepository<Blog>,
        IBlogRepository
    {        private EFCoreContext _efCoreContext;        public BlogRepository(EFCoreContext efCoreContext) : base(efCoreContext)
        {
            _efCoreContext = efCoreContext;
        }        public IEnumerable<Blog> GetList()
        {            var iQueryTable = _efCoreContext.Set<Blog>().
                FromSql("select * from Blog");            return iQueryTable.ToList();
        }
    }

 EntityFramework Core Raw SQL

 EntityFramework Core Raw SQL

下面我們來看看存儲過程。

 EntityFramework Core Raw SQL

CREATE PROCEDURE dbo.GetBlogList
AS
BEGIN
    SELECT * FROM dbo.Blog
END
GO

 EntityFramework Core Raw SQL

 EntityFramework Core Raw SQL

        public IEnumerable<Blog> GetList()
        {            var iQueryTable = _efCoreContext.Set<Blog>().
                FromSql("EXECUTE  dbo.GetBlogList");            return iQueryTable.ToList();
        }

 EntityFramework Core Raw SQL

參數查詢 

利用參數化存儲過程查詢。

 EntityFramework Core Raw SQL

* FROM dbo.Blog WHERE Id =

 EntityFramework Core Raw SQL

結果利用FromSql就變成了如下:

 EntityFramework Core Raw SQL

        public IEnumerable<Blog> GetList()
        {            var Id = new SqlParameter("Id", "1");            var iQueryTable = _efCoreContext.Set<Blog>().
                FromSql("EXEC dbo.GetBlogList {0}", 1);            return iQueryTable.ToList();
        }

 EntityFramework Core Raw SQL

上述是利用string.format的形式來傳參,我們也可以利用SqlParameter來傳參,如下:

 EntityFramework Core Raw SQL

        public IEnumerable<Blog> GetList()
        {            var Id = new SqlParameter("Id", "1");            var iQueryTable = _efCoreContext.Set<Blog>().
                FromSql("EXEC dbo.GetBlogList @id", Id);            return iQueryTable.ToList();
        }

 EntityFramework Core Raw SQL

我們通過開啟調試,可以清晰看到執行的存儲過程。

 EntityFramework Core Raw SQL

通過如上我們知道參數化查詢有兩種形式,下面我們再來看看linq查詢。

linq查詢

上述我們演示一直直接使用FromSql,其實在此之后我們可以繼續通過linq來進行查詢,如下:

 EntityFramework Core Raw SQL

        public IEnumerable<Blog> GetList()
        {            var Id = new SqlParameter("Id", "2");            var iQueryTable = _efCoreContext.Set<Blog>().
                FromSql("EXEC dbo.GetBlogList @id", Id).Where(d => d.Name == "efcore2");            return iQueryTable.ToList();
        }

 EntityFramework Core Raw SQL

之前我們映射了Blog和Post之間的關系,這里我們只能查詢出Blog表的數據,通過對上述linq的講解,我們完全可以通過inlcude來顯式加載Post表數據,如下:

 EntityFramework Core Raw SQL

        public IEnumerable<Blog> GetList()
        {            var Id = new SqlParameter("Id", "2");            var iQueryTable = _efCoreContext.Set<Blog>().
                FromSql("EXEC dbo.GetBlogList @id", Id).Include(d => d.Posts);            return iQueryTable.ToList();
        }

 EntityFramework Core Raw SQL

 EntityFramework Core Raw SQL

好吧,明確告訴我們對于存儲過程是不支持Inlude操作的,所以要想Include我們只能進行簡單的查詢,如下:

 EntityFramework Core Raw SQL

        public IEnumerable<Blog> GetList()
        {            var iQueryTable = _efCoreContext.Set<Blog>().
                FromSql("select * from blog").Include(d => d.Posts);            return iQueryTable.ToList();
        }

 EntityFramework Core Raw SQL

查找官網資料時發現居然對表值函數(TVF)是可以Include的,創建內嵌表值函數如下:

 EntityFramework Core Raw SQL

USE [EFCoreDb]
GO

IF OBJECT_ID('dbo.GetBlog') IS NOT NULL
    DROP FUNCTION dbo.GetBlog;
GO

CREATE FUNCTION dbo.GetBlog 
    (@Name VARCHAR(max)) RETURNS TABLE WITH SCHEMABINDING
AS   
RETURN
 SELECT Id, Name, Url FROM dbo.Blog WHERE Name = @Name
GO

 EntityFramework Core Raw SQL

調用如下:

 EntityFramework Core Raw SQL

        public IEnumerable<Blog> GetList()
        {            var name = "efcore2";            var iQueryTable = _efCoreContext.Set<Blog>().
                FromSql("select * from [dbo].[GetBlog] {0}", name).Include(d => d.Posts);            return iQueryTable.ToList();
        }

 EntityFramework Core Raw SQL

結果出乎意料的出現語法錯誤:

 EntityFramework Core Raw SQL

通過SQL Server Profiler查看發送的SQL語句如下:

 EntityFramework Core Raw SQL

這能不錯么,官網給的示例也是和上述一樣,如下:

 EntityFramework Core Raw SQL

只是按照和他一樣的搬過來了,未曾想太多,還是粗心大意了,想了好一會,按照我們正常調用表值函數即可,我們需要用括號括起來才行,如下:

 EntityFramework Core Raw SQL

        public IEnumerable<Blog> GetList()
        {            var name = "efcore2";            var iQueryTable = _efCoreContext.Set<Blog>().
                FromSql("select * from [dbo].[GetBlog] ({0})", name).Include(d => d.Posts);            return iQueryTable.ToList();
        }

 EntityFramework Core Raw SQL

上述將[dbo.GetBlog]和({0})隔開和挨著都可以。這個時候才不會出現語法錯誤。執行的SQL如下才是正確的。

 EntityFramework Core Raw SQL

好了,到了這里關于EF Core中原始查詢我們就告一段落了,其中還有一個知識點未談及到,在EF Core我們可以直接通過底層的ADO.NET來進行查詢,我們來看下:

底層ADO.NET查詢

 EntityFramework Core Raw SQL

        public IEnumerable<Blog> GetList()
        {            var list = new List<Blog>();            using (var connection = _efCoreContext.Database.GetDbConnection())
            {
                connection.Open();                using (var command = connection.CreateCommand())
                {
                    command.CommandText = "SELECT * FROM dbo.Blog";                    using (SqlDataReader reader = command.ExecuteReader() as SqlDataReader)
                    {                        while (reader.Read())
                        {                            var blog = new Blog();
                            blog.Id = Convert.ToInt32(reader["Id"]);
                            blog.Name = reader["Name"].ToString();
                            blog.Url = reader["Url"].ToString();
                            list.Add(blog);
                        }
                    }                      
                }
            }            return list;
        }

 EntityFramework Core Raw SQL

 EntityFramework Core Raw SQL

分享題目:EntityFrameworkCoreRawSQL
網頁路徑:http://m.newbst.com/article48/jesiep.html

成都網站建設公司_創新互聯,為您提供網站收錄標簽優化電子商務網站設計公司App設計動態網站

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都定制網站網頁設計