gpt4 book ai didi

c# - LINQ 到 SQL : Why can't I have a Zero to Many Cardinality

转载 作者:行者123 更新时间:2023-11-30 22:48:00 25 4
gpt4 key购买 nike

我在我的数据库中创建了一个外键关系,这样我就可以在这种情况下将另一个表作为属性访问 House.Area

现在,如果我创建一个 House 对象并将 Area 设为 null,我会在 SubmitChanges() 上得到以下异常:

An attempt was made to remove a relationship between a Area and a House. However, one of the relationship's foreign keys (House.AreaID) cannot be set to null.

好吧,我把上面的例子简化了一点,但是下面的评论让我觉得我应该举一个更好的例子

“House”表有一个名为 CityID 的列映射到连接到 CityID 的“City”表,因为 FK 和 CityID 不能为空

“House”表还有一个名为 AreaID 的列映射到连接 CityID 和 AreaID 的“Area”表,但 AreaID 可以为空。

房子总是需要有 1 个城市。 1个城市可以有很多房子。房子可以在零个或 1 个区域。 1 个区域可能有零个或多个房屋。

我更新了House.City = new City(....);House.CityID 确实得到一个值 <- checked

错误更新

An attempt was made to remove a relationship between a Area and a House. However, one of the relationship's foreign keys (House.CityID, House.AreaID) cannot be set to null.

除非更新 Area 覆盖了可以解释错误的 CityID 的值。请发表评论。有办法吗?

最佳答案

看起来您的数据库不允许该字段为空。在 SQL Server 企业管理器(或您正在使用的任何数据库工具)中加载 House 表并检查 AreaID 字段是否允许空值

关于c# - LINQ 到 SQL : Why can't I have a Zero to Many Cardinality,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2003904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com