日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

在.NET Core中使用MySQL5.7的JSON类型字段

發布時間:2023/12/4 数据库 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在.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类型字段的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。