Entity Framework 将外键设置为可为空后,代码首先无法更新数据库
Entity Framework code first can't update database after setting foreign key as nullable
在我将模型的外键(ProcessID 和 SubProcessID)设置为可为 null 后,我无法先使用 EF 代码更新我的数据库。
这是模型:
public class MyModel
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long MyModelID { get; set; }
[Display(Name = "Process")]
public int? ProcessID { get; set; }
[Display(Name = "Sub Process")]
public int? SubProcessID { get; set; }
//... some more properties here
public virtual Process Process { get; set; }
public virtual SubProcess SubProcess { get; set; }
}
添加迁移后的迁移代码如下:
public override void Up()
{
DropForeignKey("dbo.MyTable", "ProcessID", "dbo.Process");
DropForeignKey("dbo.MyTable", "SubProcessID", "dbo.SubProcess");
DropIndex("dbo.MyTable", new[] { "ProcessID" });
DropIndex("dbo.MyTable", new[] { "SubProcessID" });
AlterColumn("dbo.MyTable", "ProcessID", c => c.Int());
AlterColumn("dbo.MyTable", "SubProcessID", c => c.Int());
CreateIndex("dbo.MyTable", "ProcessID");
CreateIndex("dbo.MyTable", "SubProcessID");
AddForeignKey("dbo.MyTable", "ProcessID", "dbo.Process", "ProcessID");
AddForeignKey("dbo.MyTable", "SubProcessID", "dbo.SubProcess", "SubProcessID");
}
public override void Down()
{
DropForeignKey("dbo.MyTable", "SubProcessID", "dbo.SubProcess");
DropForeignKey("dbo.MyTable", "ProcessID", "dbo.Process");
DropIndex("dbo.MyTable", new[] { "SubProcessID" });
DropIndex("dbo.MyTable", new[] { "ProcessID" });
AlterColumn("dbo.MyTable", "SubProcessID", c => c.Int(nullable: false));
AlterColumn("dbo.MyTable", "ProcessID", c => c.Int(nullable: false));
CreateIndex("dbo.MyTable", "SubProcessID");
CreateIndex("dbo.MyTable", "ProcessID");
AddForeignKey("dbo.MyTable", "SubProcessID", "dbo.SubProcess", "SubProcessID", cascadeDelete: true);
AddForeignKey("dbo.MyTable", "ProcessID", "dbo.Process", "ProcessID", cascadeDelete: true);
}
我收到的错误消息是这样的:
Table 'MyDatabase.dbo.MyTable' doesn't exist
注意: 我只是在将这些外键设置为可为空的 int 时更新数据库失败。但是,如果我将其设为 int,则可以成功更新数据库。
感谢您的帮助!
如果你们中的任何人遇到与我相同的问题,这是我对我的问题的解决方案:
在“迁移”文件夹中> Configuration.cs:
我将配置构造函数更改为:
public Configuration()
{
AutomaticMigrationsEnabled = false;
SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator());
CodeGenerator = new MySql.Data.Entity.MySqlMigrationCodeGenerator();
}
现在可以更新数据库了!!
如果你们中的任何人在使用 MySQL 使用代码优先方法时遇到问题,我愿意提供帮助! :)
在我将模型的外键(ProcessID 和 SubProcessID)设置为可为 null 后,我无法先使用 EF 代码更新我的数据库。
这是模型:
public class MyModel
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long MyModelID { get; set; }
[Display(Name = "Process")]
public int? ProcessID { get; set; }
[Display(Name = "Sub Process")]
public int? SubProcessID { get; set; }
//... some more properties here
public virtual Process Process { get; set; }
public virtual SubProcess SubProcess { get; set; }
}
添加迁移后的迁移代码如下:
public override void Up()
{
DropForeignKey("dbo.MyTable", "ProcessID", "dbo.Process");
DropForeignKey("dbo.MyTable", "SubProcessID", "dbo.SubProcess");
DropIndex("dbo.MyTable", new[] { "ProcessID" });
DropIndex("dbo.MyTable", new[] { "SubProcessID" });
AlterColumn("dbo.MyTable", "ProcessID", c => c.Int());
AlterColumn("dbo.MyTable", "SubProcessID", c => c.Int());
CreateIndex("dbo.MyTable", "ProcessID");
CreateIndex("dbo.MyTable", "SubProcessID");
AddForeignKey("dbo.MyTable", "ProcessID", "dbo.Process", "ProcessID");
AddForeignKey("dbo.MyTable", "SubProcessID", "dbo.SubProcess", "SubProcessID");
}
public override void Down()
{
DropForeignKey("dbo.MyTable", "SubProcessID", "dbo.SubProcess");
DropForeignKey("dbo.MyTable", "ProcessID", "dbo.Process");
DropIndex("dbo.MyTable", new[] { "SubProcessID" });
DropIndex("dbo.MyTable", new[] { "ProcessID" });
AlterColumn("dbo.MyTable", "SubProcessID", c => c.Int(nullable: false));
AlterColumn("dbo.MyTable", "ProcessID", c => c.Int(nullable: false));
CreateIndex("dbo.MyTable", "SubProcessID");
CreateIndex("dbo.MyTable", "ProcessID");
AddForeignKey("dbo.MyTable", "SubProcessID", "dbo.SubProcess", "SubProcessID", cascadeDelete: true);
AddForeignKey("dbo.MyTable", "ProcessID", "dbo.Process", "ProcessID", cascadeDelete: true);
}
我收到的错误消息是这样的:
Table 'MyDatabase.dbo.MyTable' doesn't exist
注意: 我只是在将这些外键设置为可为空的 int 时更新数据库失败。但是,如果我将其设为 int,则可以成功更新数据库。
感谢您的帮助!
如果你们中的任何人遇到与我相同的问题,这是我对我的问题的解决方案:
在“迁移”文件夹中> Configuration.cs:
我将配置构造函数更改为:
public Configuration()
{
AutomaticMigrationsEnabled = false;
SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator());
CodeGenerator = new MySql.Data.Entity.MySqlMigrationCodeGenerator();
}
现在可以更新数据库了!!
如果你们中的任何人在使用 MySQL 使用代码优先方法时遇到问题,我愿意提供帮助! :)