entity-framework-core - .Net Core Entity Framework MySQL - 字符串字段仅存储 255 个符号

标签 entity-framework-core .net-core mysql-5.7

我正在 Windows 上使用 Visual Studio 2015 创建一个使用 .Net Core、Entity Framework 和 MySQL 5.7 的应用程序。我在 VS2015 中为使用 EF 的控制台应用程序创建了新的简单 .net-core 项目。我的数据模型由单一类型 Article 组成,具有 ID (int) 和 Body(string) 字段。当我创建数据库时:

> dotnet ef migrations add initial_migration
> dotnet ef database update

然后我的数据模型的字符串字段 (Article.Body) 在数据库中获取类型 VARCHAR(255),无论我在列属性中设置什么[Column(TypeName = "TEXT")], VARCHAR(1000) 也不起作用。因此,如果我保存一些长字符串,它会被截断为 255 个字符。

此外,如果我进入数据库设置并更改 Body 列以在 MySQL Workbench 中键入 TEXT,那么字符串仍然会被截断为 255 个字符。我做错了什么?

为了简单起见,我已将所有代码(数据库上下文、模型)放在 Program.cs 中。

程序.cs

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using MySQL.Data.EntityFrameworkCore.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;

namespace netcore
{
    public class Program
    {
        public static void Main(string[] args)
        {
            ArticlesContext context = ArticlesContext.Create();

            Article a = new Article();
            a.Body = @"If one puts some long string here, than only first 255 characters will be saved.";

            context.Add(a);
            context.SaveChanges();
        }
    }

    // DB Context
    public class ArticlesContext : DbContext
    {
        private static string _conStr = @"server=localhost;userid=sevka;pwd=12321;port=3306;database=netcore;sslmode=none;";

        public ArticlesContext() : base() { }

        public ArticlesContext(DbContextOptions<ArticlesContext> options)
        : base(options)
        { }

        public static ArticlesContext Create()
        {
            var optionsBuilder = new DbContextOptionsBuilder<ArticlesContext>();
            optionsBuilder.UseMySQL(_conStr);

            //Ensure database creation
            var context = new ArticlesContext(optionsBuilder.Options);
            context.Database.EnsureCreated();

            return context;
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseMySQL(_conStr);
        }

        public DbSet<Article> Articles { get; set; }
    }

    public class Article
    {
        public int ID { get; set; }

        [Column(TypeName = "TEXT")]
        public string Body { get; set; }
    }
}

项目.json

{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.1",
      "type": "platform"
    },
    "Microsoft.ApplicationInsights.AspNetCore": "1.0.0",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0",
    "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "MySQL.Data.Core": "7.0.4-IR-191",
    "MySql.Data.EntityFrameworkCore": "7.0.6-IR31"
  },

  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  }
}

最佳答案

根据您的反馈,您需要使用 Fluent API 而不是数据注释,因为我知道在 EF Core 中使用 Fluent API 更好,这可能会在未来版本中改变。

在数据库上下文中,您可以设置实体的映射,如下所示:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  var entity = modelBuilder.Entity<Entity>();

  entity.Property(p => p.Body).HasColumnType("text");

  base.OnModelCreating(modelBuilder);
}

如果您想要更时尚的实体映射,可以深入学习本教程:Creating Angular2 Application with ASP.NET Core Template Pack in VS 2015 ,本教程适用于带有 SQL Server 的 EF Core,但也适用于 MySql。

关于entity-framework-core - .Net Core Entity Framework MySQL - 字符串字段仅存储 255 个符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40833262/

相关文章:

c# - 在 centos 7 上使用 epoll 和 net core

azure - 什么类型的 Azure 应用服务来托管网络核心控制台应用程序?

mysql - 如何在表上形成 mysql 查询以按名为 'type' 的列和另一列的计数进行分组

mysql - 将 default_password_lifetime 变量设置为 0 后,mysql 中的过期用户无法工作

c# - 通过继承创建一个惰性 DbContext 和一个急切 DbContext?

c# - Scaffold-DbContext 实例失败 - 从现有数据库创建模型(具有个人身份的项目)

entity-framework-core - EntityFramework core group by 给了我一个命令

sql-server - EF core 2.1 中的问题映射标量函数

Docker 和网络核心应用程序不起作用

mysql - 服务器更改后 where 子句中的未知列