Pomelo.entityframeworkcore.mysql: .Net Core2 EF MySQL 在将外键列更改为可为空时出现问题

创建于 2018-02-08  ·  4评论  ·  资料来源: PomeloFoundation/Pomelo.EntityFrameworkCore.MySql

我正在开发一个应用程序,在该应用程序中我通过 Code First 方法使用 .Net Core 2、EF Core 和 MySQL 作为数据库服务器。

我有2张桌子:

  1. 用户
  2. 员工

User 表是包含用户信息的主表,Employee 表是包含 ID_User 列的子表,如下所示:

 public class User : BaseEntity
    {
        public int ID_User { get; set; }
        public string Name { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }

        public virtual ICollection<Employee> Employees{get;set;}
    }



 public class Employee : Entity
    {
        public int ID_Employee { get; set; }
        public string Name { get; set; }

        public int ID_User { get; set; }

        public virtual User User { get; set; }
    }

当我使用上面的映射并且我在两个表中都有足够的数据时,Everythihg 工作得很好。

现在,我想让 Employee 表中的列 ID_User 可以为空

为了实现此更改,我对模型进行了以下更改:

public class Employee : Entity
    {
        public int ID_Employee { get; set; }
        public string Name { get; set; }

        public int? ID_User { get; set; }

        public virtual User User { get; set; }
    }

并在映射文件中:

builder.HasOne(x=>x.User).WithMany(y=>y.Employees).HasForeignKey(z=>z.ID_User).IsRequired(false);

运行 dotnet ef migrations add empuser 命令后,它生成了以下迁移代码:

migrationBuilder.DropForeignKey(
name: "FK_Employee_User_ID_User",
表:“员工”);

    migrationBuilder.AlterColumn<int>(
        name: "ID_User",
        table: "Employee",
        nullable: true,
        oldClrType: typeof(int));

    migrationBuilder.AddForeignKey(
        name: "FK_Employee_User_ID_User",
        table: "Employee",
        column: "ID_User",
        principalTable: "User",
        principalColumn: "ID_User",
        onDelete: ReferentialAction.Restrict);

现在,当我运行 dotnet ef database update 时,它​​给了我以下错误:

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 'CONSTRAINT FK_Employee_User_ID_User' at line 1

请帮忙。

谢谢

最有用的评论

@caleblloyd ,谢谢我得到了这个修复。

实际上,我为此使用了 MySQL 官方连接器,这导致了问题。

后来我尝试了与Pomelo.EntityFrameworkCore.MySql相同的场景,它的工作

!!! 柚子岩!!!

所有4条评论

你能生成 SQL吗? 看来这是一个 SQL 语法错误,因此应该提供一些线索。

它没有生成任何 SQL,我已经尝试过了。 它给出了同样的错误
当我运行“dotnet ef migrations script”命令时

在星期四,2018年2月8日,23:16 mguinness [email protected]写道:

你能生成SQL吗
https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/#generating-a-sql-script
看来这是一个 SQL 语法错误,因此应该提供一些线索。


您收到此消息是因为您创作了该线程。
直接回复本邮件,在GitHub上查看
https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/issues/483#issuecomment-364192130
或静音线程
https://github.com/notifications/unsubscribe-auth/AIcy3L4ZQKlICMAtRFQJk8RuFTFBNWbfks5tSzLygaJpZM4R98p8
.

将“Microsoft”日志记录转为“信息”,然后运行您的迁移,它将记录 SQL。 示例(将 Microsoft: error 更改为 Microsoft: information)

https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/blob/master/test/EFCore.MySql.FunctionalTests/appsettings.json

@caleblloyd ,谢谢我得到了这个修复。

实际上,我为此使用了 MySQL 官方连接器,这导致了问题。

后来我尝试了与Pomelo.EntityFrameworkCore.MySql相同的场景,它的工作

!!! 柚子岩!!!

此页面是否有帮助?
0 / 5 - 0 等级