2016-11-27 47 views
2

我使用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 
    } 
    } 
} 
+1

您不是沒有使用Fluent API,您可以設置列類型,還可以顯示您的代碼在哪裏設置屬性的值? –

+0

@ H.Herzl,謝謝你,你說得對。我已經在您的評論之後嘗試過Fluent API,並且它工作正常。那麼看起來使用Data Annotations屬性不適用於EF Core?屬性的值在Program.cs的Main方法中設置,代碼如下: context.Add(a); context.SaveChanges(); – Vasiliy

+1

對於未來的訪問者,這是關於此問題的官方錯誤報告:https://github.com/aspnet/EntityFramework/issues/8135,它又鏈接到https://bugs.mysql.com/bug.php? ID = 84423 – Alpha

回答

2

根據您的反饋,你需要使用流利的API,而不是數據註解的,因爲我知道這是更好地使用流暢的API EF Core,這可以在未來版本中更改。

在你的數據庫環境下,你可以設置映射爲你的實體,這樣的事情:

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。