一对一关系首先在实体框架代码中失败
我正在尝试首先建立一对一关系 EF v4.1 代码。这是我的代码:
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; }
}
这工作正常,但是当我删除站时,出现级联删除错误:
“无法删除主键值,因为对此键的引用仍然存在。[外键约束名称 = System_Station]”}“
我应该如何解决此问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如错误消息告诉您的那样,与
Station
仍然存在外键关系,因此首先使用级联删除或删除System
中的相关行。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 inSystem
first.System 的主键也是 Station 的外键。如果删除Station,则需要先删除System。您还可以使用级联删除。
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.