c#实体多对多嵌套实体的计数为零

时间:2017-03-24 09:18:43

标签: c# entity-framework linq

我想从商家类型中获取商家,其中的关系是多对多的。

我有一个ID是商家类型ID,我希望获得具有此ID的所有商家类型。

商家类型应包含商家类型中的商家集合。

假设商家“a”和“b”具有id = 2

的商家类型“g”
 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"));

任何人都知道为什么我不能让商家进入商家类型以及如何解决它的问题是什么?

于28.3.2017更新

{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}

1 个答案:

答案 0 :(得分:1)

您必须交换关键列名称:

modelBuilder.Entity<Merchant>().HasMany(e => e.MerchantTypes)
            //      Left                       Right
            .WithMany(e => e.Merchants)
            .Map(m => m.ToTable("MerchantTypeMerchants")
            .MapLeftKey("Merchant_ID")          // was: MerchantType_ID
            .MapRightKey("MerchantType_ID"));   // was: Merchant_ID