以前在创建迁移时,级联删除设置为true,然后更新数据库。老实说,我没有注意到级联消除设置为真。 在应用于数据库的几次迁移之后,我需要将级联删除设置为false但它不起作用,因为它继续级联删除。
这是我所建立的一对多关系,客户可以拥有多辆车,而车辆属于一个客户:
这是我的客户类:
public class Customer {
public int Id { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
public List<Vehicle> Vehicles { get; set; }
public Customer()
{
Vehicles = new List<Vehicle>();
}
}
这是我的车辆类:
public class Vehicle {
public int Id { get; set; }
public string Enrollment{ get; set; }
public string Chassis { get; set; }
public float? Kilometers{ get; set; }
public int? CylinderCapacity{ get; set; }
public Customer Customer { get; set; }
public int CustomerId{ get; set; }
}
这是我的Fluent API配置:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Vehicle>()
.HasRequired(v => v.Customer)
.WithMany(c => c.Vehicles)
.HasForeignKey(v => v.CustomerId)
.WillCascadeOnDelete(false);
}
迁移类是这样的:
public partial class DisableDeleteOnCascade : DbMigration
{
public override void Up()
{
DropForeignKey("dbo.Vehicles", "CustomerId", "dbo.Customers");
AddForeignKey("dbo.Vehicles", "CustomerId", "dbo.Customers", "Id");
}
public override void Down()
{
DropForeignKey("dbo.Vehicles", "CustomerId", "dbo.Customers");
AddForeignKey("dbo.Vehicles", "CustomerId", "dbo.Customers", "Id", cascadeDelete: true);
}
}
由于某些原因,当我删除客户仍在删除车辆时,我不想要这种行为。我在这做错了什么?
答案 0 :(得分:0)
你建造项目吗?你应该建立或重建项目并重新执行。我想你应该清理ef的状态