实体框架代码优先:如何建模客户/地址关系?

发布于 2024-10-20 08:14:06 字数 1230 浏览 1 评论 0原文

这是我的简化模型:

public class Customer
{
    public int ID { get; set; }
    public int MailingAddressID { get; set; }
    [ForeignKey("MailingAddressID")]
    public Address MailingAddress { get; set; }

    public virtual ICollection<Address> Addresses { get; set; }
}

public class Address
{
    public int ID { get; set; }
    public int CustomerID { get; set; }
    [ForeignKey("CustomerID")]
    public Customer Customer { get; set; }
}

当我尝试创建数据库时,出现以下错误:

在表“Customers”上引入外键约束“Customer_MailingAddress”可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。 无法创建约束。请参阅以前的错误。

我不明白问题是什么。我了解具有地址的客户无法删除,并且作为客户邮寄地址的地址也无法删除。

这符合我的设计,因为如果客户有一个或多个地址,则其中一个必须是邮寄地址,并且该地址无法删除。

那么我在这里缺少什么?谢谢!

编辑:

忘记提及,我尝试在 OnModelBuilding 方法中添加以下行:

modelBuilder.Entity<Customer>().Property(x => x.MailingAddressID).IsOptional();

这允许构建数据库,但是在添加客户时出现以下错误:

INSERT 语句与 FOREIGN KEY 约束“Customer_MailingAddress”冲突。冲突发生在数据库“DomainModel.SeasideHeightsEntities”、表“dbo.Addresses”、列“ID”中。 该声明已终止。

仍然不知道如何正确建模。

Here is my simplified model:

public class Customer
{
    public int ID { get; set; }
    public int MailingAddressID { get; set; }
    [ForeignKey("MailingAddressID")]
    public Address MailingAddress { get; set; }

    public virtual ICollection<Address> Addresses { get; set; }
}

public class Address
{
    public int ID { get; set; }
    public int CustomerID { get; set; }
    [ForeignKey("CustomerID")]
    public Customer Customer { get; set; }
}

When I try and create the database I get the following error:

Introducing FOREIGN KEY constraint 'Customer_MailingAddress' on table 'Customers' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint. See previous errors.

I don't understand what the problem is. I understand that a Customer with an Address cannot be deleted and also that an Address which is a Customer's Mailing Address can also not be deleted.

This fits with my design because if a Customer has one or more addresses, then one must be a mailing address and that address can not be deleted.

So what am I missing here? Thanks!

Edit:

Forgot to mention that I tried adding the following line in the OnModelBuilding method:

modelBuilder.Entity<Customer>().Property(x => x.MailingAddressID).IsOptional();

This allows the database to be built, however when adding a customer I get the following error:

The INSERT statement conflicted with the FOREIGN KEY constraint "Customer_MailingAddress". The conflict occurred in database "DomainModel.SeasideHeightsEntities", table "dbo.Addresses", column 'ID'.
The statement has been terminated.

Still at a loss as to how to model this properly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

苦笑流年记忆 2024-10-27 08:14:06

问题是...哪个先被删除,客户还是邮寄地址?您不能同时删除它们,删除将按顺序进行。当第一个被删除时,它不符合规则 b/c,第二个尚未被删除。

从我对您的模型的了解来看,我不会使用外键来处理此逻辑,我会在对象验证期间通过在 MailingAddress 属性上放置 [Required] 属性而不是外键来处理它。

您还应该考虑其他实现逻辑,以确保 MailingAddress 是 Addresses 集合的一部分。

The problem is... which gets deleted first, the customer, or the mailing address? You can't delete them both at the same time, the deletes will happen in a sequence. When the first gets deleted, it fails the rule b/c the second hasn't been deleted yet.

From what I can see of your model, I'd not use the foreign keys to handle this logic, I'd handle it during object validation by putting a [Required] attribute on the MailingAddress property instead of the foreignkey.

You should also consider additional implementation logic to ensure that the MailingAddress is part of the Addresses collection.

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