【问题标题】:DateTime Error with mysql using dotnet core使用 dotnet 核心的 mysql 出现日期时间错误
【发布时间】:2020-10-24 18:17:59
【问题描述】:

将 Mysql 与 dotnet 核心 API 结合使用,我的实体具有 DateTime 类型列。在迁移中,它得到一个错误

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,

【问题讨论】:

  • 首先,上传您的完整CREATE 代码。
  • 我认为您的迁移器正在尝试创建您的 mysql 版本无法处理的数据类型规范。这可能是迁移器中的错误/缺乏支持,您必须使用任何选项将原始/自定义类型传递给数据库。你的 MySQL 是什么版本
  • 我的服务器版本:5.5.51-38.1,包: 我的实体代码:namespace Core.Entities { public class Materials:BaseEntity { public string Name { get;放; } 公共日期时间 CreatedAt { 获取;放; } public DateTime ModifiedAt { get;放; } 公共布尔状态 { 获取;放; } } } 我的 CLICMD:dotnet ef migrations add MaterialsAdded -p Infrastructure -s API -o Data/Migrations

标签: c# mysql .net-core migration pomelo-entityframeworkcore-mysql


【解决方案1】:

您需要指定您正在使用的服务器版本和类型,尤其是在您使用非常旧(且不受支持)版本的 MySQL 的情况下。

添加迁移时最简单的方法是在类中实现IDesignTimeDbContextFactory<TContext> 接口,该接口仅在运行 ef 核心工具时使用:

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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-01
    • 2013-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2016-04-01
    相关资源
    最近更新 更多