.Net核心實體框架MySQL - 字符串字段僅存儲255個符號

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

我正在使用Visual Studio 2015在Windows上使用.Net Core,Entity Framework和MySQL 5.7創建應用程序。我在VS2015中創建了一個新的簡單的.net-core項目,用於使用EF的控制台應用程序。我的數據模型由單一類型Article組成,它具有ID(int)和Body(字符串)字段。當我創建數據庫時:

> 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個字符上截斷字符串。我究竟做錯了什麼?

為簡單起見,我將所有代碼(db上下文,模型)放在Program.c中。

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
    }
  }
}

一般承認的答案

根據您的反饋,您需要使用流暢的API而不是數據註釋,因為我知道在EF Core中使用流暢的API會更好,這在將來的版本中會有所改變。

在db上下文中,您可以設置實體的映射,如下所示:

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

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

  base.OnModelCreating(modelBuilder);
}

如果你想擁有一個更時尚的實體映射,你可以深入學習本教程: 在VS 2015中使用ASP.NET核心模板包創建Angular2應用程序 ,本教程適用於帶有SQL Server的EF Core,但也適用於MySql。



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow