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

.Net Core Entity Framework MySQL - string fields store 255 symbols only

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

> dotnet ef migrations add initial_migration
> dotnet ef database update

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

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

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

Program.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; }
    }
}

Project.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 服务器的 EF Core,但适用于 MySql还有。