如何从 Code First 中删除外键?
How do I remove a Foreign Key from Code First?
我有两个模特。自行车和预订。
自行车:
public class Bike
{
public int Id { get; set; }
public BikeType Type { get; set; }
public BikeGender Gender { get; set; }
public BikeSize Size { get; set; }
public string Brand { get; set; }
public double HourRate { get; set; }
public double DailyRate { get; set; }
public virtual Store Store { get; set; }
[ForeignKey("Store")]
public int Store_Id { get; set; }
}
和预订:
public class Reservation
{
public int Id { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
public virtual Customer Customer { get; set; }
[ForeignKey("Customer")]
public int Customer_Id { get; set; }
public virtual List<Bike> Bikes { get; set; }
public Reservation()
{
Bikes = new List<Bike>();
}
}
当我从 Code First 创建数据库时,它会在 'Bikes' table 中添加一个名为:'Reservation_Id'.
的列
Click to view image of the problem
当我创建一个新的 Reservation 时出现问题,我选择的自行车从 Reservation 获取 ID 的 'Reservation_Id'。因此,当我尝试删除此预订时,出现错误:
SqlException: The DELETE statement conflicted with the REFERENCE
constraint "FK_dbo.Bikes_dbo.Reservations_Reservation_Id". The
conflict occurred in database "BikeRental.Model.BikeShopDataModel",
table "dbo.Bikes", column 'Reservation_Id'.
我用来删除预订的代码:
Click here to view the reservation delete code
如何解决我可以删除预订而不影响唯一密钥的问题?或者如何从 table?
中删除整个唯一键
Bike
和 Reservation
之间存在一对多关系。
EfCore 或 EF 在 Many
侧需要一个 ForeignKey
。当你没有在右边定义 ForeignKey
时,EF 会阻止你,因为那个 Reservation
对象依赖于 Bike
对象。你的错误是错误地定义了这种关系。
Bike
class 应该是这样的:
public class Bike
{
public int Id { get; set; }
public BikeType Type { get; set; }
public BikeGender Gender { get; set; }
public BikeSize Size { get; set; }
public string Brand { get; set; }
public double HourRate { get; set; }
public double DailyRate { get; set; }
public virtual Store Store { get; set; }
public List<Reservation> Reservations{get;set;}
[ForeignKey("Store")]
public int Store_Id { get; set; }
}
Reservation
class 应该是这样的:
public class Reservation
{
public int Id { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
public virtual Customer Customer { get; set; }
[ForeignKey("Customer")]
public int Customer_Id { get; set; }
public virtual Bike Bike { get; set; }
[ForeignKey("Bike")]
public int Bike_Id { get; set; }
}
我有两个模特。自行车和预订。
自行车:
public class Bike
{
public int Id { get; set; }
public BikeType Type { get; set; }
public BikeGender Gender { get; set; }
public BikeSize Size { get; set; }
public string Brand { get; set; }
public double HourRate { get; set; }
public double DailyRate { get; set; }
public virtual Store Store { get; set; }
[ForeignKey("Store")]
public int Store_Id { get; set; }
}
和预订:
public class Reservation
{
public int Id { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
public virtual Customer Customer { get; set; }
[ForeignKey("Customer")]
public int Customer_Id { get; set; }
public virtual List<Bike> Bikes { get; set; }
public Reservation()
{
Bikes = new List<Bike>();
}
}
当我从 Code First 创建数据库时,它会在 'Bikes' table 中添加一个名为:'Reservation_Id'.
的列Click to view image of the problem
当我创建一个新的 Reservation 时出现问题,我选择的自行车从 Reservation 获取 ID 的 'Reservation_Id'。因此,当我尝试删除此预订时,出现错误:
SqlException: The DELETE statement conflicted with the REFERENCE constraint "FK_dbo.Bikes_dbo.Reservations_Reservation_Id". The conflict occurred in database "BikeRental.Model.BikeShopDataModel", table "dbo.Bikes", column 'Reservation_Id'.
我用来删除预订的代码: Click here to view the reservation delete code
如何解决我可以删除预订而不影响唯一密钥的问题?或者如何从 table?
中删除整个唯一键Bike
和 Reservation
之间存在一对多关系。
EfCore 或 EF 在 Many
侧需要一个 ForeignKey
。当你没有在右边定义 ForeignKey
时,EF 会阻止你,因为那个 Reservation
对象依赖于 Bike
对象。你的错误是错误地定义了这种关系。
Bike
class 应该是这样的:
public class Bike
{
public int Id { get; set; }
public BikeType Type { get; set; }
public BikeGender Gender { get; set; }
public BikeSize Size { get; set; }
public string Brand { get; set; }
public double HourRate { get; set; }
public double DailyRate { get; set; }
public virtual Store Store { get; set; }
public List<Reservation> Reservations{get;set;}
[ForeignKey("Store")]
public int Store_Id { get; set; }
}
Reservation
class 应该是这样的:
public class Reservation
{
public int Id { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
public virtual Customer Customer { get; set; }
[ForeignKey("Customer")]
public int Customer_Id { get; set; }
public virtual Bike Bike { get; set; }
[ForeignKey("Bike")]
public int Bike_Id { get; set; }
}