一对一关系首先在实体框架代码中失败

发布于 2024-12-02 14:10:06 字数 718 浏览 6 评论 0原文

我正在尝试首先建立一对一关系 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蒲公英的约定 2024-12-09 14:10:06

正如错误消息告诉您的那样,与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 in System first.

内心旳酸楚 2024-12-09 14:10:06

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文