我正在使用Entity Framework 4 CTP5 Code First,并且有一个类似的模型:

public class User {
   public int UserId { get; set; }
   public string Email { get; set; }
   public ICollection<Customer> TaggedCustomers { get; set; }
}
public class Customer {
  public int CustomerId { get; set; }
  public string FirstName { get; set; }
  public string LastName { get; set; }
  public ICollection<User> TaggedBy { get; set; }
}


在用户可以“标记”客户,而客户可以被许多用户“标记”的多对多关系中。我有一个正常工作的DbContext,可以查询客户使用

 var customers = DbContext.Customers.Include(c => c.TaggedBy);


但是每个客户都将拥有所有已标记该客户的用户。如何限制TaggedBy集合仅以指定的UserId生成结果?

我已经尝试过DbContext.Customers.Include(c => c.TaggedBy.Select(x => x.Id == userId));的方法,但这会引发错误。

最佳答案

EF Feature CTP5: Fluent API Samples - ADO.NET team blog - Site Home - MSDN Blogs

modelBuilder.Entity<Product>()
    .HasMany(p => p.Tags)
    .WithMany(t => t.Products)
    .Map(m =>
        {
            m.MapLeftKey(p => p.ProductId, "CustomFkToProductId");
            m.MapRightKey(t => t.TagId, "CustomFkToTagId");
        });


Code First Mapping Changes in CTP5 - ADO.NET team blog - Site Home - MSDN Blogs

modelBuilder.Entity<Product>()
    .HasMany(p => p.SoldAt)
    .WithMany(s => s.Products)
    .Map(mc => {
        mc.ToTable("ProductsAtStores");
        mc.MapLeftKey(p => p.Id, "ProductId");
        mc.MapRightKey(s => s.Id, "StoreId");
    });

10-04 21:42