gpt4 book ai didi

c# - 多重性与参照约束冲突

转载 作者:IT王子 更新时间:2023-10-29 04:21:46 25 4
gpt4 key购买 nike

我收到以下 EF 错误:

Agent_MailingAddress: : Multiplicity conflicts with the referential constraint in Role 'Agent_MailingAddress_Target' in relationship 'Agent_MailingAddress'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be 1

似乎在它执行时抛出这个 base.OnModelCreating(modelBuilder).

这是我的模型。 FWIW,Agent继承自 User类。

public class Agent
{
public int AgentId { get; set; }
public int PrimaryAddressId { get; set; }
public Address PrimaryAddress { get; set; }
public int? MailingAddressId { get; set; }
public Address MailingAddress { get; set; }
}

public class Address
{
public int AddressId { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
}

我认为这个问题与以下事实有关 Agent拥有不止一处类型为 Address 的特性也可能是因为其中之一可以为空。我进行了一些搜索,但似乎找不到答案。

我想改变我的 Agent模型具有类型为 List<Address> 的单个属性那将使用 UserAddresses查找表会解决错误,但我更愿意保留当前模型而不是。

我该如何解决这个错误?提前致谢。

最佳答案

如果您的配置和您的型号不匹配,就会发生这种情况。

假设在您的数据库配置中您有这样的规则:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Agent>().HasRequired(x=>x.MailingAddress);
//..

但是在您的模型中您说 MailingAddress 是可选的:

public int? MailingAddressId { get; set; }

I believe the issue has something to do with the fact that Agent has more than one property of type Address and possibly also because one of them is nullable

事实并非如此。

关于c# - 多重性与参照约束冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32459263/

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