Har*_*rma 6 .net mysql entity-framework .net-core asp.net-core
我正在开发一个应用程序,我通过Code First方法使用.Net Core 2,EF Core和MySQL作为数据库服务器.
我有2张桌子:
用户表是包含用户信息的主表,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; }
}
Run Code Online (Sandbox Code Playgroud)
当我使用上面的映射并且两个表中都有足够的数据时,一切都很完美.
现在,我想将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; }
}
Run Code Online (Sandbox Code Playgroud)
并在映射文件中:
builder.HasOne(x=>x.User).WithMany(y=>y.Employees).HasForeignKey(z=>z.ID_User).IsRequired(false);
Run Code Online (Sandbox Code Playgroud)
运行该dotnet ef migrations add empuser
命令后,它生成以下迁移代码:
migrationBuilder.DropForeignKey(
name: "FK_Employee_User_ID_User",
table: "Employee");
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);
Run Code Online (Sandbox Code Playgroud)
现在,当我运行dotnet ef database update
它时,给我以下错误:
您的SQL语法有错误; 检查与MySQL服务器版本对应的手册,以便
FK_Employee_User_ID_User
在第1行的"CONSTRAINT " 附近使用正确的语法
请帮忙.
谢谢
小智 5
尝试将SQL语句直接放入MySQL Workbench。
当我将EF core 2与MySQL结合使用时遇到类似的错误,这可以帮助我更好地理解问题并帮助解决问题。(对我来说,这是输入错误)。您至少可以使用此方法来确定是迁移中还是SQL语句中的错误。
我知道这不是一个具体的解决方案,但是我希望这可以帮助您理解并解决问题:)
归档时间: |
|
查看次数: |
1365 次 |
最近记录: |