Giter VIP home page Giter VIP logo

ef-json-x's Introduction

Entity Framework Core 1.1. - Getting started with JSON fields in SQL Server

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.

JSON fields in Blogging table

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.

Contents

About this sample
Before you begin
Run this sample
Sample details
Related links

About this sample

  • 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

Before you begin

To run this sample, you need the following prerequisites.

Software prerequisites:

  1. SQL Server 2016 (or higher) or an Azure SQL Database

  2. ASP.NET Core 1.0 SDK (or higher) and Entity Framework Core

  3. Optional: Visual Studio 2015 Update 3 (or higher) or Visual Studio Code Editor.

Azure prerequisites:

  1. Permission to create an Azure SQL Database

Run this sample

  1. Create a database on SQL Server 2016 or Azure SQL Database.

  2. 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.

  3. Locate Setup.cs file and change connection string in the ConfigureServices method: var connection = @"Server=.;Database=Blogging;Trusted_Connection=True;";

  4. 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.

  5. 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.

Sample details

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.

Related Links

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.

Code of Conduct

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.

License

These samples and templates are all licensed under the MIT license. See the license.txt file in the root.

Questions

Email questions to: [[email protected]](mailto: [email protected]).

ef-json-x's People

Contributors

ittoot avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.