diff --git a/samples/features/json/Entity-Framework/Controllers/BlogsController.cs b/samples/features/json/Entity-Framework/Controllers/BlogsController.cs index 16d71a6b..4376c8e2 100644 --- a/samples/features/json/Entity-Framework/Controllers/BlogsController.cs +++ b/samples/features/json/Entity-Framework/Controllers/BlogsController.cs @@ -1,6 +1,5 @@ using EFGetStarted.AspNetCore.NewDb.Models; using Microsoft.AspNetCore.Mvc; -using Microsoft.EntityFrameworkCore; using System.Linq; namespace EFGetStarted.AspNetCore.NewDb.Controllers @@ -18,6 +17,22 @@ namespace EFGetStarted.AspNetCore.NewDb.Controllers { return View(_context.Blogs.ToList()); } + + public IActionResult Search(string Owner) + { + // Option 1: .Net side filter using LINQ: + var blogs = _context.Blogs + .Where(b => b.Owner.Name == Owner) + .ToList(); + + // Option 2: SQL Server filter using T-SQL: + //var blogs = _context.Blogs + // .FromSql(@"SELECT * FROM Blogs + // WHERE JSON_VALUE(Owner, '$.Name') = {0}", Owner) + // .ToList(); + + return View("Index", blogs); + } public IActionResult Create() { diff --git a/samples/features/json/Entity-Framework/Models/Models.cs b/samples/features/json/Entity-Framework/Models/Models.cs index 376011ce..e0d2a4d7 100644 --- a/samples/features/json/Entity-Framework/Models/Models.cs +++ b/samples/features/json/Entity-Framework/Models/Models.cs @@ -1,5 +1,4 @@ using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata; using Newtonsoft.Json; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; @@ -40,7 +39,7 @@ namespace EFGetStarted.AspNetCore.NewDb.Models [NotMapped] public string[] Tags { - get { return (this._Tags == null) ? null : JsonConvert.DeserializeObject(this._Tags); } + get { return _Tags == null ? null : JsonConvert.DeserializeObject(_Tags); } set { _Tags = JsonConvert.SerializeObject(value); } } diff --git a/samples/features/json/Entity-Framework/Startup.cs b/samples/features/json/Entity-Framework/Startup.cs index 059fd7a5..cb4656bd 100644 --- a/samples/features/json/Entity-Framework/Startup.cs +++ b/samples/features/json/Entity-Framework/Startup.cs @@ -1,10 +1,10 @@ -using Microsoft.AspNetCore.Builder; +using EFGetStarted.AspNetCore.NewDb.Models; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -using EFGetStarted.AspNetCore.NewDb.Models; -using Microsoft.EntityFrameworkCore; namespace BackingFields { diff --git a/samples/features/json/Entity-Framework/Views/Shared/_Layout.cshtml b/samples/features/json/Entity-Framework/Views/Shared/_Layout.cshtml index 1d611ffe..2c039198 100644 --- a/samples/features/json/Entity-Framework/Views/Shared/_Layout.cshtml +++ b/samples/features/json/Entity-Framework/Views/Shared/_Layout.cshtml @@ -26,8 +26,17 @@ - Getting Started with JSON and EF + Getting Started with JSON and EF +
diff --git a/samples/features/json/Entity-Framework/sql-scripts/setup.sql b/samples/features/json/Entity-Framework/sql-scripts/setup.sql index 2326390f..c97fb8f6 100644 --- a/samples/features/json/Entity-Framework/sql-scripts/setup.sql +++ b/samples/features/json/Entity-Framework/sql-scripts/setup.sql @@ -30,3 +30,11 @@ INSERT INTO Blogs (Url, Tags, Owner) VALUES ('http://blogs.msdn.com/visualstudio', '[".Net", "VS"]','{"Name":"Jack","Surname":"Doe","Email":"jack.doe@contoso.com"}'), ('https://blogs.msdn.microsoft.com/sqlserverstorageengine/', '["SQL Server"]','{"Name":"Mike","Surname":"Doe","Email":"mike.doe@contoso.com"}') +-- Add indexing on Name property in JSON column: +ALTER TABLE Blogs + ADD OwnerName AS JSON_VALUE(Owner, '$.Name'); + +CREATE INDEX ix_OwnerName + ON Blogs(OwnerName); + +