0

Using Mysql with dotnet core API, my entity has on DateTime type column. In migration, it getting an error as

MySql.Data.MySqlClient.MySqlException (0x80004005): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(6) NOT NULL,
    `ModifiedAt` datetime(6) NOT NULL,
3
  • 2
    First, upload your full CREATE code. Commented Jul 4, 2020 at 5:09
  • I think your migrator is trying to create a datatype specification that your version of mysql cannot cope with. That's probably a bug/lack of support in the migrator that you'll have to work round with whatever option it has to pass a raw/custom type to the db. What version is your MySQL Commented Jul 4, 2020 at 7:35
  • My Server version: 5.5.51-38.1, Packages: <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.3"/> <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.1"/> My Entity Code: namespace Core.Entities { public class Materials : BaseEntity { public string Name { get; set; } public DateTime CreatedAt { get; set; } public DateTime ModifiedAt { get; set; } public bool Status { get; set; } } } My CLICMD: dotnet ef migrations add MaterialsAdded -p Infrastructure -s API -o Data/Migrations Commented Jul 6, 2020 at 15:02

1 Answer 1

1

You need to specify the server version and type you are using, especially in your case, where you are using a very old (and unsupported) version of MySQL.

The easiest way to do this when adding migrations, is to implement the IDesignTimeDbContextFactory<TContext> interface in a class, that will only be used when running the ef core tools:

public class BloggingDesignTimeContextFactory : IDesignTimeDbContextFactory<BloggingContext>
{
    public BloggingContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<BloggingContext>();

        optionsBuilder.UseMySql(
            "server=127.0.0.1;port=3306;user=root;password=;database=so62725308",
            mySqlOptions => mySqlOptions
                .ServerVersion(new Version(5, 5, 51), ServerType.MySql)); // <-- the server version

        return new BloggingContext(optionsBuilder.Options);
    }
}
Sign up to request clarification or add additional context in comments.

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.