Entity framework core .Net核心实体框架MySQL-字符串字段仅存储255个符号
我正在使用Visual Studio 2015在Windows上使用.Net Core、Entity Framework和MySQL 5.7创建一个应用程序。我在VS2015中使用EF为控制台应用程序创建了新的简单.net核心项目。我的数据模型由单一类型Article组成,其中包含ID(int)和Body(string)字段。创建数据库时,请执行以下操作:Entity framework core .Net核心实体框架MySQL-字符串字段仅存储255个符号,entity-framework-core,.net-core,mysql-5.7,Entity Framework Core,.net Core,Mysql 5.7,我正在使用Visual Studio 2015在Windows上使用.Net Core、Entity Framework和MySQL 5.7创建一个应用程序。我在VS2015中使用EF为控制台应用程序创建了新的简单.net核心项目。我的数据模型由单一类型Article组成,其中包含ID(int)和Body(string)字段。创建数据库时,请执行以下操作: > dotnet ef migrations add initial_migration > dotnet ef databas
> dotnet ef migrations add initial_migration
> dotnet ef database update
然后,我的数据模型的字符串字段(Article.Body)在数据库中获得VARCHAR(255)类型,无论我在列属性[Column(TypeName=“TEXT”)]
中设置了什么,VARCHAR(1000)都不能正常工作。因此,如果我保存一些长字符串,它将被截断为255个字符
此外,若我转到数据库设置并在MySQL Workbench中更改Body列以键入文本,那个么静态字符串将被截断为255个字符。我做错了什么
为了简单起见,我将所有代码(db上下文、模型)都放在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; }
}
}
根据您的反馈,您需要使用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);
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
var entity=modelBuilder.entity();
Property(p=>p.Body).HasColumnType(“文本”);
基于模型创建(modelBuilder);
}
如果你想有一个更时尚的实体映射,你可以深入到本教程:,本教程适用于EF Core with SQL Server,但也适用于MySql。如果你没有使用Fluent API,你可以设置列类型,还可以显示你的代码在哪里设置属性值?@H.Herzl,谢谢,你说得对。在你的评论之后,我尝试了fluentapi,它工作正常。所以,使用数据注释属性似乎不是EF Core的发展方向?属性值在代码中Program.cs的Main方法中设置:context.Add(a);SaveChanges();对于未来的访问者,这是关于此问题的官方错误报告:,它反过来链接到
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var entity = modelBuilder.Entity<Entity>();
entity.Property(p => p.Body).HasColumnType("text");
base.OnModelCreating(modelBuilder);
}