유동적 인 linq를 사용하여 카트에 현재 매핑되지 않은 모든 Catalog
객체를 가져 오려고합니다. 매핑 테이블 cart_catalog_mapping
은 EF에 의해 생성됩니다. 다음 도메인 객체를 사용합니다.현재 유창한 linq을 사용하여 매핑 테이블에없는 객체를 얻는 방법
카트 (질문에
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_mapping 테이블을 만들었 음을 인식합니다. Catalog
테이블에있는 참조가없는 모든 행을 가져 오려고합니다.cart_catalog_mapping
테이블. 내가 상상 해요 SQL은
SELECT * FROM Catalog WHERE Catalog.Id NOT IN (SELECT Catalog_Id FROM cart_catalog_mapping)
나는이를 얻기 위해 사용하는 시도한 유창 LINQ는 다음입니다 : 공공 IList의 GetAllUnassociatedCatalogs() { IRepository _catalogRepository; 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의 어떤 요소가 없습니다.
가 거의 잊었다 내가 그 장바구니 가정 및 카탈로그있어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);
}
}
이 훨씬 쉬울 것 : 당신은 '카탈로그에서 카탈로그에서 '라고 말할 수있는 곳은? catalog.Carts.Any() select catalog'입니다. 당신이 그 이유가없는 이유가 있습니까? – hvd
카트 : 카탈로그는 many-to-many 또는 one-to-many입니까? – IronMan84
장바구니는 많은 카탈로그를 가질 수 있지만 한 카탈로그는 장바구니에만 연결할 수 있다는 점에서 일대 다 관계입니다 –