Catalog
流暢な linq を使用して、現在カートにマップされていないすべてのオブジェクトを取得しようとしています。マッピング テーブルcart_catalog_mapping
は EF によって生成されます。次のドメイン オブジェクトを使用します。
カート (質問に関係のないコメントや余分なフィールドを削除
public partial class Cart : BaseEntity, ILocalizedEntity
{
public virtual string Name { get; set; }
public virtual DateTime OpeningDateUtc { get; set; }
public virtual DateTime ClosingDateUtc { get; set; }
public virtual bool IsReadonly { get; set; }
public virtual bool IsPreviewMode { get; set; }
public virtual CustomerRole CustomerType { get; set; }
public virtual ICollection<Catalog> Catalogs { get; set; }
}
カタログ(質問に関係のないコメントと余分なフィールドを再度削除しました)
public partial class Catalog : BaseEntity, ILocalizedEntity
{
public virtual string Name { get; set; }
public virtual string Code { get; set; }
public virtual bool Published { get; set; }
public virtual int DisplayOrder { get; set; }
}
コードを使用した EF5 は、最初に Cart と Catalog テーブルを作成します。また、カートにリストがあることを認識し、cart_catalog_mapping テーブルを作成します。Catalog
テーブルに参照がないテーブル内のすべての行を取得しようとしていcart_catalog_mapping
ます。私が思い描いているSQLは
SELECT * FROM Catalog WHERE Catalog.Id NOT IN (SELECT Catalog_Id FROM cart_catalog_mapping)
これらを取得するために使用しようとした流暢な linq は次のとおりです。IRepository _cartRepository;
var query = from catalog in _catalogRepository.Table
from cart in _cartRepository.Table
where !cart.Catalogs.Contains(catalog)
select catalog;
return query.ToList();
}
残念ながら、返された IList には要素がありません。(現在、cart または cart_catalog_mapping テーブルには行がなく、catalog テーブルには 3 行あります)
ほとんど忘れていた、マッピングテーブル
public partial class CartMap : EntityTypeConfiguration<Cart>
{
public CartMap()
{
this.ToTable("Cart");
this.HasKey(c => c.Id);
this.Property(c => c.Name).IsRequired().HasMaxLength(100);
this.Property(c => c.IsOnline).IsRequired();
this.HasMany(c => c.Catalogs)
.WithMany()
.Map(m => m.ToTable("Cart_Catalog_Mapping"));
}
}
public partial class CatalogMap : EntityTypeConfiguration<Catalog>
{
public CatalogMap()
{
this.ToTable("Catalog");
this.HasKey(c => c.Id);
this.Property(c => c.Name).IsRequired().HasMaxLength(100);
}
}