我正在 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/