2016-11-27 5 views
2

Visual Studio 2015를 사용하여 .Net Core, Entity Framework 및 MySQL 5.7, Windows에서 응용 프로그램을 만들고 있습니다. EF를 이용한 콘솔 어플리케이션을위한 VS2015의 핵심 프로젝트. 내 데이터 모델은 ID (int) 및 본문 (문자열) 필드가있는 단일 유형, 아티클로 구성됩니다. 나는 데이터베이스를 작성할 때 일 :.Net Core Entity Framework MySQL - 문자열 필드에 255 기호 만 저장

다음
> dotnet ef migrations add initial_migration 
> dotnet ef database update 

문자열 필드 (Article.Body를) 내 데이터 모델의 것은 아무리 내가 열 속성 [Column(TypeName = "TEXT")], VARCHAR (1000)하지 작업에 설정된 것을, 데이터베이스 VARCHAR (255)를 입력하지 도착 게다가. 그래서 긴 문자열을 저장하면 255 자로 잘립니다.

또한 데이터베이스 설정으로 이동하여 Body 열을 변경하여 MySQL Workbench에서 TEXT를 입력하면 여전히 문자열이 255 자에서 잘립니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

간단하게하기 위해 Program.cs에 모든 코드 (db 컨텍스트, 모델)를 넣었습니다. 나는 그것이 유창 API를 사용하는 것이 좋습니다 알고

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

@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를 사용할 필요가 EF Core는 향후 버전에서 변경 될 수 있습니다. 이런 당신이 당신의 엔티티에 대한 매핑을 설정할 수 있습니다 데시벨의 맥락에서

, 뭔가 : Creating Angular2 Application with ASP.NET Core Template Pack in VS 2015,이 튜토리얼 : 좀 더 세련된 엔티티 매핑을 갖고 싶어

protected override void OnModelCreating(ModelBuilder modelBuilder) 
{ 
    var entity = modelBuilder.Entity<Entity>(); 

    entity.Property(p => p.Body).HasColumnType("text"); 

    base.OnModelCreating(modelBuilder); 
} 

, 당신은이 튜토리얼에 뛰어 수 있습니다 SQL Server에서 EF Core 용이지만 MySql에도 적용됩니다.