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 I开发者_运维技巧d { 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?
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.
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.
精彩评论