我正在使用.NET Core,Entity Framework和MySQL 5.7,在Windows上使用Visual Studio 2015创建一个应用程序。使用EF应用程序应用程序。我的数据模型由具有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中键入文本,则仍将字符串截断为255个字符。我在做什么错?
为了简单起见
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中使用Fluent API,这可能会在以后的版本中更改。
在您的DB上下文中,您可以为您的实体设置映射,类似的内容:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var entity = modelBuilder.Entity<Entity>();
entity.Property(p => p.Body).HasColumnType("text");
base.OnModelCreating(modelBuilder);
}
如果您想拥有一个更时尚的实体映射,则可以潜入本教程:使用ASP.NET Core Template Pack在VS 2015中创建Angular2应用程序,此教程适用于具有SQL Server的EF Core,但也适用于MySQL。