This project contains an example implementation of ASP.NET MVC application that shows how to serialize/de-serialize complex fields in Entity Framework model as JSON fields in SQL database. JSON support enables you to represent complex user defined structures such as objects and arrays in cells as JSON object.
This way, you don't need to create separate tables for each complex object that you have in your classes. In this example, rows from Blog table with JSON fields are mapped to the Blog class using Tags and Owner properties. Owner is object of Person type that don't have separate table.
public class Blog
{
public int BlogId { get; set; }
[Url]
public string Url { get; set; }
public List<Post> Posts { get; set; }
public string[] Tags { get; set; }
public Person Owner { get; set; }
}
public class Person
{
[Required]
public string Name { get; set; }
public string Surname { get; set; }
[EmailAddress]
public string Email { get; set; }
}
In this example, you can see how to read and store JSON fields from the database into EF model objects, show them and store them in ASP.NET MVC.
About this sample
Before you begin
Run this sample
Sample details
Related links
- Applies to: SQL Server 2016 (or higher), Azure SQL Database
- Key features: JSON support in SQL Server, Entity Framework, ASP.NET MVC.
- Programming Language: C#, ASP.NET Core, Transact-SQL
- Authors: Jovan Popovic
To run this sample, you need the following prerequisites.
Software prerequisites:
-
SQL Server 2016 (or higher) or an Azure SQL Database
-
ASP.NET Core 1.0 SDK (or higher) and Entity Framework Core
-
Optional: Visual Studio 2015 Update 3 (or higher) or Visual Studio Code Editor.
Azure prerequisites:
- Permission to create an Azure SQL Database
-
Create a database on SQL Server 2016 or Azure SQL Database.
-
From SQL Server Management Studio or Visual Studio/Sql Server Data Tools connect to your SQL Server 2016 or Azure SQL database and execute setup.sql script that will create and populate Blogs and Posts tables.
-
Locate Setup.cs file and change connection string in the ConfigureServices method:
var connection = @"Server=.;Database=Blogging;Trusted_Connection=True;";
-
Build the project - Open command prompt in project root folder (the folder that contains project.json file), and run following commands: dotnet restore to take all necessary NuGet packages, dotnet build to build the project. As an alternative, open the EFGettingStarted.xproj file from the root directory using Visual Studio 2015 Update 3 (or higher). Restore packages using right-click menu on the project in Visual Studio and by choosing Restore Packages item.
-
Run the sample app using F5 or Ctrl+F5 in Visual Studio 2015, or using dotnet run executed in the command prompt of the project root folder.
This sample shows how to read and write complex fields such as string arrays and objects in the columns in the JSON format. JSON fields in database are stored as NVARCHAR fields and contain JSON representation of array and object.
JSON data from database are serialized/de-serialized through properties in Blog entity.
Blog fields are shown and inserted using ASP.NET MVC framework.
You can find more information about the components that are used in this sample on these locations: .Net Core download Entity Framework Core 1.0.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.
These samples and templates are all licensed under the MIT license. See the license.txt file in the root.
Email questions to: [[email protected]](mailto: [email protected]).