Reputation: 6428
I am trying to build a One to one relationship EF v4.1 Code first. This is my code:
public class System
{
[Key, ForeignKey("Station") ]
public int Id { get; set; }
[DisplayName("Last Polled")]
public DateTime? LastPolledOn { get; set; }
public virtual Station Station { get; set; }
}
public class Station
{
public int Id { get; set; }
public int Status { get; set; }
public string FullName
{
get
{
return StoreNumber + " - " + StoreName;
}
}
public virtual System System{ get; set; }
}
This works fine but when I delete Station, I get cascade delete error:
"The primary key value cannot be deleted because references to this key still exist. [ Foreign key constraint name = System_Station ]"}"
What should I do to resolve this?
Upvotes: 2
Views: 928
Reputation: 3994
The primary key of System is also the foreign key to Station. If you delete Station you need to delete System first. You can also use cascaded delete.
Upvotes: 1
Reputation: 23113
As the error message tells you, there is still a foreign key relation to the Station
, so eighter use cascaded delete or delete the relating rows in System
first.
Upvotes: 2