1. <legend id='3w5l2'><style id='3w5l2'><dir id='3w5l2'><q id='3w5l2'></q></dir></style></legend>

      <bdo id='3w5l2'></bdo><ul id='3w5l2'></ul>

    1. <tfoot id='3w5l2'></tfoot>
      <i id='3w5l2'><tr id='3w5l2'><dt id='3w5l2'><q id='3w5l2'><span id='3w5l2'><b id='3w5l2'><form id='3w5l2'><ins id='3w5l2'></ins><ul id='3w5l2'></ul><sub id='3w5l2'></sub></form><legend id='3w5l2'></legend><bdo id='3w5l2'><pre id='3w5l2'><center id='3w5l2'></center></pre></bdo></b><th id='3w5l2'></th></span></q></dt></tr></i><div id='3w5l2'><tfoot id='3w5l2'></tfoot><dl id='3w5l2'><fieldset id='3w5l2'></fieldset></dl></div>

      <small id='3w5l2'></small><noframes id='3w5l2'>

    2. 如何使用 Pomelo.EntityFramework 的 JsonObject

      How to use JsonObject of Pomelo.EntityFramework(如何使用 Pomelo.EntityFramework 的 JsonObject)

            <tbody id='BCs4f'></tbody>
          <tfoot id='BCs4f'></tfoot>
            <bdo id='BCs4f'></bdo><ul id='BCs4f'></ul>

            <legend id='BCs4f'><style id='BCs4f'><dir id='BCs4f'><q id='BCs4f'></q></dir></style></legend>

              1. <small id='BCs4f'></small><noframes id='BCs4f'>

                <i id='BCs4f'><tr id='BCs4f'><dt id='BCs4f'><q id='BCs4f'><span id='BCs4f'><b id='BCs4f'><form id='BCs4f'><ins id='BCs4f'></ins><ul id='BCs4f'></ul><sub id='BCs4f'></sub></form><legend id='BCs4f'></legend><bdo id='BCs4f'><pre id='BCs4f'><center id='BCs4f'></center></pre></bdo></b><th id='BCs4f'></th></span></q></dt></tr></i><div id='BCs4f'><tfoot id='BCs4f'></tfoot><dl id='BCs4f'><fieldset id='BCs4f'></fieldset></dl></div>
                本文介绍了如何使用 Pomelo.EntityFramework 的 JsonObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想将字符串列表作为 json 存储到 mysql 表中.我看到 pomelo entityframework 对此提供了支持.我跟着这个 https://libraries.io/github/tuanbs/Pomelo.EntityFrameworkCore.MySql

                I want to store list of string into mysql table as json. I saw there is support for this in pomelo entityframework. I followed this https://libraries.io/github/tuanbs/Pomelo.EntityFrameworkCore.MySql

                这是我的实体

                public class Project
                {
                   public int Id {get;set;}
                
                   public string Title {get;set;}
                
                   public JsonObject<List<string>> Tags {get;set;}
                }
                

                但是当 _context.Database.EnsureDeleted(); 被调用时,它给出以下错误

                But when _context.Database.EnsureDeleted(); is called it gives below error

                实体类型项目"上的导航属性标签"不是虚拟的.UseLazyLoadingProxies 要求所有实体类型都是公开的,未密封,具有虚拟导航属性,并具有公共或受保护的构造函数.

                Navigation property 'Tags' on entity type 'Project' is not virtual. UseLazyLoadingProxies requires all entity types to be public, unsealed, have virtual navigation properties, and have a public or protected constructor.

                但它不是导航属性,我必须添加虚拟关键字,而是一列.不知道我在这里错过了什么.

                But it is not navigation property that I have to add virtual keyword with it but is a column. Don't know what am I missing here.

                推荐答案

                看下面的示例代码,它取自 我在我们的 GitHub 存储库上的帖子,并且没有问题:

                Take a look at the following sample code, that is taken from my post on our GitHub repository, and works without issues:

                using System;
                using System.Collections.Generic;
                using System.Diagnostics;
                using System.Linq;
                using Microsoft.EntityFrameworkCore;
                using Microsoft.Extensions.Logging;
                using Pomelo.EntityFrameworkCore.MySql.Storage;
                
                namespace IssueConsoleTemplate
                {
                    public class IceCream
                    {
                        public int IceCreamId { get; set; }
                        public string Name { get; set; }
                        public JsonObject<Energy> Energy { get; set; }
                        public JsonObject<List<string>> Comments { get; set; }
                    }
                
                    public class Energy
                    {
                        public double Kilojoules { get; set; }
                        public double Kilocalories { get; set; }
                    }
                
                    public class Context : DbContext
                    {
                        public virtual DbSet<IceCream> IceCreams { get; set; }
                
                        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
                        {
                            optionsBuilder
                                .UseMySql("server=127.0.0.1;port=3306;user=root;password=;database=So62301095",
                                    b => b.ServerVersion(new ServerVersion("8.0.20-mysql")))
                                .UseLoggerFactory(LoggerFactory.Create(b => b
                                    .AddConsole()
                                    .AddFilter(level => level >= LogLevel.Information)))
                                .EnableSensitiveDataLogging()
                                .EnableDetailedErrors();
                        }
                    }
                
                    internal class Program
                    {
                        private static void Main()
                        {
                            using (var context = new Context())
                            {
                                context.Database.EnsureDeleted();
                                context.Database.EnsureCreated();
                
                                context.IceCreams.AddRange(
                                    new IceCream
                                    {
                                        Name = "Vanilla",
                                        Energy = new Energy
                                        {
                                            Kilojoules = 866.0,
                                            Kilocalories = 207.0
                                        },
                                        Comments = new List<string>
                                        {
                                            "First!",
                                            "Delicious!"
                                        }
                                    },
                                    new IceCream
                                    {
                                        Name = "Chocolate",
                                        Energy = new Energy
                                        {
                                            Kilojoules = 904.0,
                                            Kilocalories = 216.0
                                        },
                                        Comments = new List<string>
                                        {
                                            "My husband likes this one a lot."
                                        }
                                    });
                
                                context.SaveChanges();
                            }
                
                            using (var context = new Context())
                            {
                                var result = context.IceCreams
                                    .OrderBy(e => e.IceCreamId)
                                    .ToList();
                
                                Debug.Assert(result.Count == 2);
                
                                Debug.Assert(result[0].Name == "Vanilla");
                                Debug.Assert(result[0].Energy.Object.Kilojoules == 866.0);
                                Debug.Assert(result[0].Comments.Object.Count == 2);
                                Debug.Assert(result[0].Comments.Object[0] == "First!");
                            }
                        }
                    }
                }
                

                它生成以下 SQL:

                info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
                      Entity Framework Core 3.1.3 initialized 'Context' using provider 'Pomelo.EntityFrameworkCore.MySql' with options: ServerVersion 8.0.20 MySql SensitiveDataLoggingEnabled DetailedErrorsEnabled
                
                info: Microsoft.EntityFrameworkCore.Database.Command[20101]
                      Executed DbCommand (81ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
                
                      DROP DATABASE `So62301095`;
                
                info: Microsoft.EntityFrameworkCore.Database.Command[20101]
                      Executed DbCommand (12ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
                
                      CREATE DATABASE `So62301095`;
                
                info: Microsoft.EntityFrameworkCore.Database.Command[20101]
                      Executed DbCommand (66ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
                
                      CREATE TABLE `IceCreams` (
                          `IceCreamId` int NOT NULL AUTO_INCREMENT,
                          `Name` longtext CHARACTER SET utf8mb4 NULL,
                          `Energy` json NULL,
                          `Comments` json NULL,
                          CONSTRAINT `PK_IceCreams` PRIMARY KEY (`IceCreamId`)
                      );
                
                info: Microsoft.EntityFrameworkCore.Database.Command[20101]
                      Executed DbCommand (15ms) [Parameters=[@p0='["First!","Delicious!"]', @p1='{"Kilojoules":866.0,"Kilocalories":207.0}', @p2='Vanilla' (Size = 4000)], CommandType='Text', CommandTimeout='30']
                
                      INSERT INTO `IceCreams` (`Comments`, `Energy`, `Name`)
                      VALUES (@p0, @p1, @p2);
                      SELECT `IceCreamId`
                      FROM `IceCreams`
                      WHERE ROW_COUNT() = 1 AND `IceCreamId` = LAST_INSERT_ID();
                
                info: Microsoft.EntityFrameworkCore.Database.Command[20101]
                      Executed DbCommand (1ms) [Parameters=[@p0='["My husband likes this one a lot."]', @p1='{"Kilojoules":904.0,"Kilocalories":216.0}', @p2='Chocolate' (Size = 4000)], CommandType='Text', CommandTimeout='30']
                
                      INSERT INTO `IceCreams` (`Comments`, `Energy`, `Name`)
                      VALUES (@p0, @p1, @p2);
                      SELECT `IceCreamId`
                      FROM `IceCreams`
                      WHERE ROW_COUNT() = 1 AND `IceCreamId` = LAST_INSERT_ID();
                
                info: Microsoft.EntityFrameworkCore.Database.Command[20101]
                      Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
                
                      SELECT `i`.`IceCreamId`, `i`.`Comments`, `i`.`Energy`, `i`.`Name`
                      FROM `IceCreams` AS `i`
                      ORDER BY `i`.`IceCreamId`
                

                仔细查看 IceCream.Comments 属性,它完全符合您的要求.

                Take a close look at the IceCream.Comments property, that does exactly what you want.

                在下面的同一 GitHub 问题上,您会发现 另一个由我发布,有一个更复杂的例子.

                On the same GitHub issue further below, you find another post by me, with a much more sophisticated example.

                此外,我们接下来将为 Pomelo 实现完整的 JSON 支持(可能在一周内).

                Also, we are going to implement full JSON support next for Pomelo (probably within a week).

                这篇关于如何使用 Pomelo.EntityFramework 的 JsonObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to delete duplicate records in mysql database?(如何删除mysql数据库中的重复记录?)
                Python Pandas write to sql with NaN values(Python Pandas 使用 NaN 值写入 sql)
                MySQL Insert amp; Joins(MySQL 插入 amp;加入)
                MySQL concat() to create column names to be used in a query?(MySQL concat() 创建要在查询中使用的列名?)
                NodeJS responded MySQL timezone is different when I fetch directly from MySQL(当我直接从 MySQL 获取时,NodeJS 响应 MySQL 时区不同)
                WHERE datetime older than some time (eg. 15 minutes)(WHERE 日期时间早于某个时间(例如 15 分钟))
                    <legend id='vvJj3'><style id='vvJj3'><dir id='vvJj3'><q id='vvJj3'></q></dir></style></legend>
                  1. <i id='vvJj3'><tr id='vvJj3'><dt id='vvJj3'><q id='vvJj3'><span id='vvJj3'><b id='vvJj3'><form id='vvJj3'><ins id='vvJj3'></ins><ul id='vvJj3'></ul><sub id='vvJj3'></sub></form><legend id='vvJj3'></legend><bdo id='vvJj3'><pre id='vvJj3'><center id='vvJj3'></center></pre></bdo></b><th id='vvJj3'></th></span></q></dt></tr></i><div id='vvJj3'><tfoot id='vvJj3'></tfoot><dl id='vvJj3'><fieldset id='vvJj3'></fieldset></dl></div>
                      <bdo id='vvJj3'></bdo><ul id='vvJj3'></ul>
                        <tbody id='vvJj3'></tbody>
                      <tfoot id='vvJj3'></tfoot>

                      <small id='vvJj3'></small><noframes id='vvJj3'>