1

I'm using ASP.NET Core 3.1 with Entity Framework. I'm trying to store byte arrays into a database. Of course, my MySQL database should support that, but Entity Framework doesn't seem to work.

Consider the following model:

public class User 
{
    [Key]
    public int Id { get; set; }
    public string Username { get; set; }
    public string Email { get; set; }
    public byte[] Salt { get; set; }
    public byte[] Password { get; set; }

    [NotMapped]
    public string PlainPassword { get; set; }
}

Now, whenever I use the package manager console to do Add-Migration InitialCreate, I get the following output:

PM> Add-Migration InitialCreate
Build started...
Build failed.

Whenever I change the byte[] to string, it works, but the passwords are no strings. What should I do? Convert them to strings or is there another workaround? And if I should convert them to strings, what is the best way to convert them?

8
  • 2
    --verbose flag will let you know what error prevents build from success Commented Oct 3, 2020 at 13:44
  • I used the verbose flag, but the output still doesn't clear things up: PM> Add-Migration InitialCreate -Verbose Using project 'to-do-list-api'. Using startup project 'to-do-list-api'. Build started... Build failed. Commented Oct 3, 2020 at 13:47
  • 1
    your code is completely fine, something else is broken. check full stacktrace, I think you are missing something that verbose mentions. I created gist with your model showing that it is possible to generate migration gist.github.com/pwrigshi/ff5ccd5a4437d054b6712ec24d089d84 Commented Oct 3, 2020 at 14:01
  • So on yours it's working. Could the problem maybe be that I am using MySQL instead of the default MSSQL? Commented Oct 3, 2020 at 14:08
  • 1
    hard to tell without details. the workaround you could use is to store byte array as hexadecimal string. this way it is implemented in EF Core Identity, which you could use to replace your entity completely Commented Oct 3, 2020 at 14:15

2 Answers 2

1

Ok for this to work with MySQL I used the gist project that @YegorAndrosov made here https://gist.github.com/pwrigshi/ff5ccd5a4437d054b6712ec24d089d84 and for MySQL i Added only 2 Packages from NuGet (i tried Microsoft.EntityFrameworkCore package but it doesn't have UseMySQL in it's DbContextOptionsBuilder and i'm no expert in EF to solve this missing part)

here are the 2 packages that work

MySql.Data.EntityFrameworkCore (it's from Oracle !)

Microsoft.EntityFrameworkCore.Design

as i mentioned above the package (Microsoft.EntityFrameworkCore) doesn't seems to have a way to use MySQL, now the only change i made from that code in the link is pointing to MySQL instead of SqlServer in DbContext, and the ConnectionString itself ofcourse to match the MySQL server in my machine)

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseMySQL("Server=localhost;Port=3306;Database=test;Uid=mysql;Pwd=MyPass;");
        
        base.OnConfiguring(optionsBuilder);
    }

then on package manager console i did

    PM> dotnet ef migrations add Init --project ConsoleApp1 --verbose

and after migration created i did

    PM> dotnet ef database update --project ConsoleApp1 --verbose

it worked just fine, migration was created, then database created on MySQL server successfully.

screenshot of the Table created in MySQL

Sign up to request clarification or add additional context in comments.

Comments

0

Using Entity Framework Core 6.0 Preview 6 you can have pre-convention model configuration. That is you can configure for example that all strings will be stored as byte arrays:

protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
  {
      configurationBuilder.Properties<string>()
          .HaveConversion<byte[]>()
          .HaveMaxLength(255);

      configurationBuilder.IgnoreAny<INonPersisted>();
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.