Updated EF JSON sample

Aded search, JSON index, cleaned-up code.
This commit is contained in:
Jovan Popovic
2017-01-15 17:50:27 +01:00
parent 20486d03b3
commit 0d039ac0c9
5 changed files with 38 additions and 7 deletions
@@ -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<Blog>(@"SELECT * FROM Blogs
// WHERE JSON_VALUE(Owner, '$.Name') = {0}", Owner)
// .ToList();
return View("Index", blogs);
}
public IActionResult Create()
{
@@ -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<string[]>(this._Tags); }
get { return _Tags == null ? null : JsonConvert.DeserializeObject<string[]>(_Tags); }
set { _Tags = JsonConvert.SerializeObject(value); }
}
@@ -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
{
@@ -26,8 +26,17 @@
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a asp-area="" asp-controller="Blog" asp-action="Index" class="navbar-brand">Getting Started with JSON and EF</a>
<a asp-area="" asp-controller="Blogs" asp-action="Index" class="navbar-brand">Getting Started with JSON and EF</a>
</div>
<div id="navbar" class="navbar-collapse collapse navbar-form">
<ul class="nav navbar-nav pull-right">
<li class="pull-right">
<form method="get" action="/Blogs/Search" class="pull-right">
<input type="text" name="Owner" placeholder="Blogs by owner" />
</form>
</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
<div class="container body-content">
@@ -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);