在.NET Core中使用MySQL5.7的JSON类型字段
Getting Started
① Adding pomelo myget feed into your NuGet.config which located in your solution root.
<?xml version="1.0" encoding="utf-8"?><configuration><packageSources><add key="nuget.org" value="https://www.nuget.org/api/v2/" /><add key="Pomelo" value="https://www.myget.org/F/pomelo/api/v2/" /></packageSources><disabledPackageSources /></configuration>② Add Pomelo.Data.MySql and Pomelo.EntityFrameworkCore.MySql into your project.json. The versions of them are 1.0.0.
③ If you have already installed the pomelo packages: Pomelo.Data.MySql and Pomelo.EntityFrameworkCore.MySql, please remove them and restore again(dotnet restore --no-cache). ?The packages are located in C:\Users\YOURNAME\.nuget\packages.
④ To define json field in model with System.JsonObject<T> will store this field as a json column.
Sample
using System;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace MySqlTest
{
? ? public class Blog
? ? {
? ? ? ? public Guid Id { get; set; }
? ? ? ? [MaxLength(32)]
? ? ? ? public string Title { get; set; }
? ? ? ? public string Content { get; set; }
? ? ? ? public JsonObject<string[]> Tags { get; set; } // Json storage
? ? }
? ? public class MyContext : DbContext
? ? {
? ? ? ? public DbSet<Blog> Blogs { get; set; }
? ? ? ? protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
? ? ? ? ? ? => optionsBuilder
? ? ? ? ? ? ? ? .UseMySql(@"Server=localhost;database=ef;uid=root;pwd=19931101;");
? ? }
? ? public class Program
? ? {
? ? ? ? public static void Main()
? ? ? ? {
? ? ? ? ? ? using (var context = new MyContext())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? // Create database
? ? ? ? ? ? ? ? context.Database.EnsureCreated();
? ? ? ? ? ? ? ? // Init sample data
? ? ? ? ? ? ? ? var blog1 = new Blog {
? ? ? ? ? ? ? ? ? ? Title = "Title #1",
? ? ? ? ? ? ? ? ? ? Tags = new string[] { "ASP.NET Core", "MySQL", "Pomelo" }
? ? ? ? ? ? ? ? };
? ? ? ? ? ? ? ? context.Add(blog1);
? ? ? ? ? ? ? ? var blog2 = new Blog
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? Title = "Title #2",
? ? ? ? ? ? ? ? ? ? Tags = new string[] { "ASP.NET Core", "MySQL" }
? ? ? ? ? ? ? ? };
? ? ? ? ? ? ? ? context.Add(blog2);
? ? ? ? ? ? ? ? context.SaveChanges();
? ? ? ? ? ? ? ? // Detect changes test
? ? ? ? ? ? ? ? blog1.Title = "Changed Title #1";
? ? ? ? ? ? ? ? context.SaveChanges();
? ? ? ? ? ? ? ? // Output data
? ? ? ? ? ? ? ? var ret = context.Blogs
? ? ? ? ? ? ? ? ? ? .Where(x => x.Tags.Object.Contains("Pomelo"))
? ? ? ? ? ? ? ? ? ? .ToList();
? ? ? ? ? ? ? ? foreach (var x in ret)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? Console.WriteLine($"{ x.Id } { x.Title }");
? ? ? ? ? ? ? ? ? ? Console.Write("[Tags]: ");
? ? ? ? ? ? ? ? ? ? foreach(var y in x.Tags.Object)
? ? ? ? ? ? ? ? ? ? ? ? Console.Write(y + " ");
? ? ? ? ? ? ? ? ? ? Console.WriteLine();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? Console.Read();
? ? ? ? }
? ? }
}
For discussion, you can comment on https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/issues/14
相關文章:
全球首發免費的MySql for Entity Framework Core
.NET Core 使用Dapper 操作MySQL
原文地址:http://www.1234.sh/post/use-json-field-type-in-net-core
.NET社區新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關注
總結
以上是生活随笔為你收集整理的在.NET Core中使用MySQL5.7的JSON类型字段的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我为什么用docker-compose来
- 下一篇: Asp.net Core基于MVC框架实