多对多关系和创建视图

时间:2019-09-16 20:32:39

标签: c# asp.net-core many-to-many

我有一个对象select t1.mid, t1.pid, if(t2.id is null, t1.pName, concat_ws(' ', t2.firstname, t2.lastname)) as pName, t1.phoneno from A.ABC t1 left join B.XYZ t2 on t1.pid = 0 and t1.phoneno in (t2.phoneno, t2.cellphone) ; ,它有2个属性(Route)。施主和代理机构均为(Donor and Agency)类型。在数据库中,我有Organizationrelationship的多对多Route<>Donor设置。当我尝试运行我的代码时,它告诉我需要为这些关系之一设置一个ForeignKey,但是此时我有点迷失了。我这样做对吗?我想念什么?

这是路线类别...

Route<>Agency

这是组织类别...

public partial class Route
{
    public Route()
    {
        RouteOrg = new HashSet<RouteOrg>();
    }

    public int RouteId { get; set; }
    public int RouteOrgId { get; set; }

    public virtual ICollection<RouteOrg> RouteOrg { get; set; }
    public List<Organization> Donors { get; set; }
    public List<Organization> Agencies { get; set; }

}

这是我的OnModelCreating方法...

public partial class Organization
{
    public Organization()
    {
        RouteOrgAgency = new HashSet<RouteOrg>();
        RouteOrgDonor = new HashSet<RouteOrg>();
    }

    public int OrgId { get; set; }
    public bool IsSelected { get; set; }
    public int? RouteId { get; set; }

    public virtual ICollection<RouteOrg> RouteOrgAgency { get; set; }
    public virtual ICollection<RouteOrg> RouteOrgDonor { get; set; }
}

我目前拥有“创建”视图,可以正确显示所有内容。我正在使用捐赠者 protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.HasAnnotation("ProductVersion", "2.2.6-servicing-10079"); modelBuilder.Entity<Organization>(entity => { entity.HasKey(e => e.OrgId); entity.HasIndex(e => e.AddressId); entity.HasIndex(e => e.OrganizationTypeId); entity.Property(e => e.OrgId).HasColumnName("OrgId"); entity.Property(e => e.AddressId).HasColumnName("AddressId"); entity.Property(e => e.DateAdded).HasDefaultValueSql("(getutcdate())"); entity.Property(e => e.Name).HasMaxLength(100); entity.Property(e => e.Nickname).HasMaxLength(50); entity.Property(e => e.OrganizationTypeId).HasColumnName("OrganizationTypeId"); entity.Property(e => e.UserId) .HasColumnName("UserId") .HasMaxLength(450); }); modelBuilder.Entity<Route>(entity => { entity.HasIndex(e => e.DayOfWeekId); entity.HasIndex(e => e.FrequencyId); entity.HasIndex(e => e.TimeOfDayId); entity.Property(e => e.RouteId).HasColumnName("RouteId"); entity.Property(e => e.DateAdded).HasDefaultValueSql("(getutcdate())"); entity.Property(e => e.DayOfWeekId).HasColumnName("DayOfWeekId"); entity.Property(e => e.FrequencyId).HasColumnName("FrequencyId"); entity.Property(e => e.RouteOrgId).HasColumnName("RouteOrgId"); entity.Property(e => e.TimeOfDayId).HasColumnName("TimeOfDayId"); }); modelBuilder.Entity<RouteOrg>(entity => { entity.Property(e => e.RouteOrgId).HasColumnName("RouteOrgId"); entity.Property(e => e.AgencyId).HasColumnName("AgencyId"); entity.Property(e => e.DonorId).HasColumnName("DonorId"); entity.Property(e => e.RouteId).HasColumnName("RouteId"); entity.HasOne(d => d.Agency) .WithMany(p => p.RouteOrgAgency) .HasForeignKey(d => d.AgencyId) .OnDelete(DeleteBehavior.ClientSetNull) .HasConstraintName("FK_RouteOrg_Agency"); entity.HasOne(d => d.Donor) .WithMany(p => p.RouteOrgDonor) .HasForeignKey(d => d.DonorId) .OnDelete(DeleteBehavior.ClientSetNull) .HasConstraintName("FK_RouteOrg_Donor"); entity.HasOne(d => d.Route) .WithMany(p => p.RouteOrg) .HasForeignKey(d => d.RouteId) .OnDelete(DeleteBehavior.ClientSetNull) .HasConstraintName("FK_RouteOrg_Route"); }); } 和代理机构check boxes列表显示多对多关系。我希望能够根据需要选择每种类型的organizations,将其保存到DB,并能够进行edit, view,等并保留所有数据

更新:现在尝试运行项目时,我也收到此错误...

System.AggregateException:'发生一个或多个错误。 (“组织”和“ Route.Agencies”之间的关系以及“组织”和“ Route.Donors”之间的关系都可以使用{'RouteId'}作为外键。要解决此问题,请至少在以下一项上显式配置外键属性关系。)'

1 个答案:

答案 0 :(得分:1)

如果您使用的是EF Core,请注意EF Core 不会会自动为您映射多对多关系。

由于一个Route将有许多RouteOrgs(而不是许多DonorsAgencies),因此您不应该配置两个Donors和{{ 1}}属性作为表Agencies的字段。

public partial class Route
{
    ...
    public List<Organization> Donors { get; set; }
    public List<Organization> Agencies { get; set; }
}

或者,如果愿意,可以为这两个字段配置Route属性:

[NotMapped]

要获取路线的捐助者和代理商,请使用public partial class Route { ... public int? RouteOrgId { get; set; } public virtual ICollection<RouteOrg> RouteOrg { get; set; } [NotMapped] public IEnumerable<Organization> Donors { get { return this.RouteOrg.Select(ro => ro.Donor); } } [NotMapped] public IEnumerable<Organization> Agencies { get{ return this.RouteOrg.Select( ro => ro.Agency); } } } 进行以下查询:

Include().ThenInclude()