Create an NVARCHAR column in MySQL using code first?

.net c# entity-framework entity-framework-6 mysql

Question

So, unlike SQL Server, in MySQL, the following property creates a column of type LONGTEXT:

public string Name { get; set; }

I want to have an NVARCHAR(255) column instead, so I tried the following:

//[DataType("NVARCHAR"), StringLength(255)]     // creates a varchar(255) column.
[DataType("NVARCHAR"), MaxLength(255)]          // creates a varchar(255) column.
public string Name { get; set; }

I also tried the following with Fluent API:

modelBuilder.Entity<MyEntity>()
    .Property(p => p.Name)
    .HasColumnType("NVARCHAR")
    .HasMaxLength(255);             // still creates a varchar(255) column.

..and:

modelBuilder.Entity<MyEntity>()
    .Property(p => p.Name)
    .HasColumnType("NVARCHAR(255)");

Which causes an InvalidOperationException with the following message:

The store type 'NVARCHAR(255)' could not be found in the MySql provider manifest

Is there something I'm doing wrong? What is the correct way to create an nvarchar column using code-first?

1
4
4/11/2018 2:24:58 PM

Accepted Answer

MySql does not have NVARCHAR type. For alternative solutions consult: mysql equivalent data types

2
4/11/2018 2:31:13 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow