我想從商家類型中獲得商家,其中的關係是多對多的。c#實體多對多嵌套實體數爲零
我得到了一個id,這是商家類型id,我想獲得所有具有此ID的商家類型。
商家類型應具有商家類型內的Merchant集合。
咱們說商人「A」和「B」有商家類型「G」使用id = 2
var result=db.MerchantTypes.Where(p => p.ID == id)
.Include(c => c.Merchants).FirstOrDefault();
上面的代碼應該返回我商家類型的結果,裏面有商家的列表。
public partial class MerchantType
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public MerchantType()
{
Merchants = new HashSet<Merchant>();
}
public int ID { get; set; }
public string Type { get; set; }
public string Description { get; set; }
public bool IsDeleted { get; set; }
public DateTime LastUpdatedDate { get; set; }
public string LastUpdatedBy { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Merchant> Merchants { get; set; }
}
public partial class Merchant
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Merchant()
{
MerchantTypes = new HashSet<MerchantType>();
}
public int ID { get; set; }
public string Name { get; set; }
public string LogoImgPath { get; set; }
public string SliderImgPath { get; set; }
public string SliderLink { get; set; }
public bool IsDeleted { get; set; }
public DateTime LastUpdatedDate { get; set; }
public string LastUpdatedBy { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertieShouldBeReadOnly")]
public virtual ICollection<MerchantType> MerchantTypes { get; set; }
}
這是商家類型和商家之間的關係。 哪個商家可以有多種類型和類型裏面可以有很多商家。 (多對多關係)。
modelBuilder.Entity<Merchant>()
.HasMany(e => e.MerchantTypes)
.WithMany(e => e.Merchants)
.Map(m => m.ToTable("MerchantTypeMerchants")
.MapLeftKey("MerchantType_ID")
.MapRightKey("Merchant_ID"));
任何人都知道究竟是爲什麼我不能得到商家類型裏面的商戶和如何解決它的問題?
更新於2017年3月28日
{SELECT
[Project1].[ID] AS [ID],
[Project1].[Type] AS [Type],
[Project1].[Description] AS [Description],
[Project1].[IsDeleted] AS [IsDeleted],
[Project1].[LastUpdatedDate] AS [LastUpdatedDate],
[Project1].[LastUpdatedBy] AS [LastUpdatedBy],
[Project1].[C1] AS [C1],
[Project1].[ID1] AS [ID1],
[Project1].[Name] AS [Name],
[Project1].[LogoImgPath] AS [LogoImgPath],
[Project1].[SliderImgPath] AS [SliderImgPath],
[Project1].[SliderLink] AS [SliderLink],
[Project1].[IsDeleted1] AS [IsDeleted1],
[Project1].[LastUpdatedDate1] AS [LastUpdatedDate1],
[Project1].[LastUpdatedBy1] AS [LastUpdatedBy1]
FROM (SELECT
[Extent1].[ID] AS [ID],
[Extent1].[Type] AS [Type],
[Extent1].[Description] AS [Description],
[Extent1].[IsDeleted] AS [IsDeleted],
[Extent1].[LastUpdatedDate] AS [LastUpdatedDate],
[Extent1].[LastUpdatedBy] AS [LastUpdatedBy],
[Join1].[ID] AS [ID1],
[Join1].[Name] AS [Name],
[Join1].[LogoImgPath] AS [LogoImgPath],
[Join1].[SliderImgPath] AS [SliderImgPath],
[Join1].[SliderLink] AS [SliderLink],
[Join1].[IsDeleted] AS [IsDeleted1],
[Join1].[LastUpdatedDate] AS [LastUpdatedDate1],
[Join1].[LastUpdatedBy] AS [LastUpdatedBy1],
CASE WHEN ([Join1].[Merchant_ID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
FROM [dbo].[MerchantTypes] AS [Extent1]
LEFT OUTER JOIN (SELECT [Extent2].[Merchant_ID] AS [Merchant_ID], [Extent3].[ID] AS [ID], [Extent3].[Name] AS [Name], [Extent3].[LogoImgPath] AS [LogoImgPath], [Extent3].[SliderImgPath] AS [SliderImgPath], [Extent3].[SliderLink] AS [SliderLink], [Extent3].[IsDeleted] AS [IsDeleted], [Extent3].[LastUpdatedDate] AS [LastUpdatedDate], [Extent3].[LastUpdatedBy] AS [LastUpdatedBy]
FROM [dbo].[MerchantTypeMerchants] AS [Extent2]
INNER JOIN [dbo].[Merchants] AS [Extent3] ON [Extent3].[ID] = [Extent2].[MerchantType_ID]) AS [Join1] ON [Extent1].[ID] = [Join1].[Merchant_ID]
WHERE [Extent1].[ID] = @p__linq__0
) AS [Project1]
ORDER BY [Project1].[ID] ASC, [Project1].[C1] ASC}
有你加入modelBuilder.Entity().Property(X => x.ID) .HasColumnName( 「MERCHANTID」); –
CPR43
我得到了但不同於你的modelBuilder.Entity() .HasMany(e => e.MerchantTypes) .WithMany(e => e.Merchants) .Map(m => m.ToTable( 「MerchantTypeMerchants」) .MapLeftKey(「MerchantType_ID」) .MapRightKey(「Merchant_ID」)); –
kyorilys