2021年3月22日星期一

EF Core - Join table without the joint field on each individual Entity

I have a property on my entity GroceryItem called GroceryItemGroceryStores which exists for the sole purpose of letting Entity Framework Core know to create a join table. My other entity of the same join table GroceryStore has the same property. I don't actually want my two entities to have this property as it just leads to a never-ending cycle where a GroceryItemGroceryStore has an Establishment (GroceryStore) which has a GroceryItemGroceryStore which has the same Establishment (GroceryStore) and it continues forever. How do I delete the property GroceryItemGroceryStores from GroceryItem and GroceryStore, but keep the join table? The entity having infinite depth:

enter image description here

namespace Vepo.Domain  {      public class GroceryItem : AVeganItem<GroceryItemTag, GroceryStore>      {          [Required]          public string Name { get; set; }          [Required]          public string Brand { get; set; }          [Required]          public string Description { get; set; }          public string Image { get; set; }          [NotMapped]          public List<GroceryStore> Establishments { get; set; }          [NotMapped]          public virtual ICollection<GroceryItemGroceryStore> GroceryItemGroceryStores { get; set; }        }  }    namespace Vepo.Domain  {      [Serializable]      public abstract class AVeganItem<VeganItemTagType, EstablishmentType> : ADomainEntity<int>      {          [Required]          public int IsNotVeganCount { get; set; }          [Required]          public int IsVeganCount { get; set; }          [Required]          public int RatingsCount { get; set; }          [Required]          public int Rating { get; set; }          [Required]          public List<VeganItemTagType> Tags { get; set; }          [Required]          public int CurrentRevisionId { get; set; }        }  }    namespace Vepo.Domain  {      [Serializable]      public abstract class ADomainEntity<IdType>      {          public IdType Id { get; set; }        }  }  namespace Vepo.Domain  {      [Serializable]      public class GroceryStore : AEstablishment<GroceryItem>      {          public virtual ICollection<GroceryItemGroceryStore> GroceryItemGroceryStores { get; set; }        }  }  

This is the join table. Just view its superclass. As you can see it has a few extra fields. Maybe that complicates things?

namespace Vepo.Domain  {      [Serializable]      public class GroceryItemGroceryStore : AVeganItemEstablishment<GroceryItem, GroceryStore>      {      }  }  namespace Vepo.Domain  {      [Serializable]      public abstract class AVeganItemEstablishment<VeganItemType, EstablishmentType> : ADomainEntity<int>      {          [Key, Column(Order = 0), Required]          public int VeganItemId { get; set; }          [Key, Column(Order = 1), Required]          public int EstablishmentId { get; set; }            public virtual VeganItemType VeganItem { get; set; }          public virtual EstablishmentType Establishment { get; set; }            [Required]          public int NotInEstablishmentCount { get; set; }          [Required]          public int InEstablishmentCount { get; set; }          [Required]          public double Price { get; set; }        }  }  

database context:

modelBuilder.Entity<GroceryItemGroceryStore>()              .HasOne(gigs => gigs.VeganItem)              .WithMany(vi => vi.GroceryItemGroceryStores)              .HasForeignKey(gigs => gigs.VeganItemId);          modelBuilder.Entity<GroceryItemGroceryStore>()              .HasOne(gigs => gigs.Establishment)              .WithMany(e => e.GroceryItemGroceryStores)              .HasForeignKey(gigs => gigs.EstablishmentId);          modelBuilder.Entity<GroceryItemGroceryStore>(gigs =>          {              gigs.HasIndex(e => new { e.VeganItemId, e.EstablishmentId }).IsUnique();          });  
https://stackoverflow.com/questions/66757226/ef-core-join-table-without-the-joint-field-on-each-individual-entity March 23, 2021 at 12:19PM

没有评论:

发表评论