postgresql 多对多映射返回错误
Posted
技术标签:
【中文标题】postgresql 多对多映射返回错误【英文标题】:postgresql mapping many to many returns error 【发布时间】:2020-05-16 09:10:01 【问题描述】:您好,我正在为我当前的映射寻找一些有用的提示。当我尝试添加迁移时,它返回以下错误。我不太确定我做错了什么,或者这不是我应该如何映射它们。
System.Reflection.TargetInvocationException:已抛出异常 通过调用的目标。 ---> System.InvalidOperationException: 无法在“Brokers.ManifestToBrokers”和 'ManifestToBroker.ACASBroker',因为已经存在关系 在“Brokers.ManifestToBrokers”和“ManifestToBroker.ABIBroker”之间。 导航属性只能参与单个关系。
我有 3 个表,Manifest、ManifestToBroker、经纪人。
public class Manifest
public virtual ManifestToBroker ManifestToBroker get; set;
.......other stuff
public class ManifestToBroker
public virtual Manifest Manifest get; set;
public int ManifestId get; set;
public int? ABIFilerId get; set;
public int? ACASFilerId get; set;
public int? AMSFilerId get; set;
public virtual Brokers ABIBroker get; set;
public virtual Brokers ACASBroker get; set;
public virtual Brokers AMSBroker get; set;
public class Brokers
[StringLength(32)]
[Required(AllowEmptyStrings = false)]
public string Name get; set;
[StringLength(50)]
public string Description get; set;
[StringLength(100)]
public string Contacts get; set;
[Required]
public int BrokerType get; set;
[Required]
public bool SupportABI get; set;
[Required]
public bool SupportACAS get; set;
[Required]
public bool SupportAMS get; set;
public virtual ICollection<ManifestToBroker> ManifestToBrokers get; set; = new List<ManifestToBroker>();
为简单起见,我已将所有映射放在 ManifestToBrokerMapper 中。
public class ManifestToBrokerMapping : IntKeyBaseBaseEntityConfiguration<ManifestToBroker>
protected override void ConfigureThis(EntityTypeBuilder<ManifestToBroker> builder)
builder.HasKey(m => m.Id);
builder.HasOne(m => m.ABIBroker)
.WithMany(m => m.ManifestToBrokers)
.HasForeignKey(m => m.ABIFilerId);
builder.HasOne(m => m.ACASBroker)
.WithMany(m => m.ManifestToBrokers)
.HasForeignKey(m => m.ACASFilerId);
builder.HasOne(m => m.AMSBroker)
.WithMany(m => m.ManifestToBrokers)
.HasForeignKey(m => m.AMSFilerId);
builder.HasOne(m => m.Manifest)
.WithOne(m => m.ManifestToBroker)
.IsRequired();
builder.HasIndex(m => new m.ManifestId, m.ABIFilerId ).IsUnique();
builder.HasIndex(m => new m.ManifestId, m.ACASFilerId ).IsUnique();
builder.HasIndex(m => new m.ManifestId, m.AMSFilerId ).IsUnique();
【问题讨论】:
【参考方案1】:发现问题,brokers表还需要包含每个ManifestToBroker
public virtual ICollection<ManifestToBroker> ABIFilerMTB get; set; = new List<ManifestToBroker>();
public virtual ICollection<ManifestToBroker> ACASFilerMTB get; set; = new List<ManifestToBroker>();
public virtual ICollection<ManifestToBroker> AMSFilerMTB get; set; = new List<ManifestToBroker>();
【讨论】:
以上是关于postgresql 多对多映射返回错误的主要内容,如果未能解决你的问题,请参考以下文章
将嵌套的 JSON 从 PostgreSQL 的多对多连接表返回到 node.js