我使用Visual Studio 2015在Windows上创建了一个使用.Net Core,Entity Framework和MySQL 5.7的应用程序。我创建了一个新的简单的.net-使用EF进行控制台应用程序的VS2015核心项目。我的数据模型由具有ID(int)和Body(字符串)字段的单个类型Article组成。当我创建的数据库来执行:我的数据模型的.Net核心实体框架MySQL - 字符串字段仅存储255个符号
> 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个字符。我究竟做错了什么?
为了简单起见,我已将所有代码(db context,model)放入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,您可以设置列类型,还可以显示您的代码在哪里设置属性的值? –
@ H.Herzl,谢谢你,你说得对。我已经在您的评论之后尝试过Fluent API,并且它工作正常。那么看起来使用Data Annotations属性不适用于EF Core?属性的值在Program.cs的Main方法中设置,代码如下: context.Add(a); context.SaveChanges(); – Vasiliy
对于未来的访问者,这是关于此问题的官方错误报告:https://github.com/aspnet/EntityFramework/issues/8135,它又链接到https://bugs.mysql.com/bug.php? ID = 84423 – Alpha