Sto creando un'applicazione utilizzando .Net Core, Entity Framework e MySQL 5.7, su Windows, utilizzando Visual Studio 2015. Ho creato un nuovo semplice progetto .net-core in VS2015 per l'applicazione console utilizzando EF. Il mio modello di dati è costituito da un singolo tipo, Articolo, che ha campi ID (int) e Corpo (stringa). Quando creo database facendo:
> dotnet ef migrations add initial_migration
> dotnet ef database update
quindi il campo stringa (Article.Body) del mio modello di dati ottiene il tipo VARCHAR (255) nel database, indipendentemente da ciò che imposto nell'attributo Column [Column(TypeName = "TEXT")]
, VARCHAR (1000) non funziona bene. Quindi, se salvo una stringa lunga, viene troncata a 255 caratteri.
Inoltre, se vado alle impostazioni del database e modifico la colonna Corpo per digitare TEXT in MySQL Workbench, la stringa ancora viene troncata su 255 caratteri. Che cosa sto facendo di sbagliato?
Ho messo tutto il codice (contesto, modello db) in Program.cs per semplicità.
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
}
}
}
In base al tuo feedback, devi utilizzare API fluente anziché annotazioni di dati, poiché so che è meglio utilizzare l'API fluente in EF Core, questo può cambiare nelle versioni future.
Nel tuo contesto db puoi impostare la mappatura per la tua entità, qualcosa di simile a questo:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var entity = modelBuilder.Entity<Entity>();
entity.Property(p => p.Body).HasColumnType("text");
base.OnModelCreating(modelBuilder);
}
Se si desidera avere una mappatura delle entità più elegante, è possibile approfondire questo tutorial: Creazione dell'applicazione Angular2 con Template Template principale ASP.NET in VS 2015 , questo tutorial è per EF Core con SQL Server ma si applica anche a MySql.