首先在实体框架代码中进行多对多映射

发布于 2024-10-08 13:14:37 字数 788 浏览 5 评论 0原文

我正在使用 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)); 但这会引发错误。

I'm using Entity Framework 4 CTP5 Code First and I have a model along the lines of:

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

There is a many to many relationship where a User can 'tag' a Customer and a Customer can be 'tagged' by many users. I have a working DbContext and I can query customers using

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

But each customer will have all users that have tagged the customer. How do I restrict the TaggedBy collection to just result with a specifed UserId?

I've tried along the lines of DbContext.Customers.Include(c => c.TaggedBy.Select(x => x.Id == userId)); but that throws an error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪异←思 2024-10-15 13:14:37

EF 功能 CTP5:流畅的 API 示例 - ADO.NET 团队博客 - 网站主页 - MSDN 博客

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");
        }); 

CTP5 中的代码优先映射更改 - ADO.NET 团队博客 - 网站主页 - MSDN 博客

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");
    });

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");
    });
执手闯天涯 2024-10-15 13:14:37

将您的集合标记为虚拟,然后您可以轻松地执行以下操作:

public class User
{
    public int UserId { get; set; }
    public string Email { get; set; }
    public virtual ICollection<Customer> TaggedCustomers { get; set; }
}

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

using(var context = new MyDbContext())
{
    var user = context.Users.Single(o => o.UserId == 0);
    var customers = user.TaggedCustomers;
}

在我看来,结果会更清晰的代码。

Mark your collections as virtual and then you can easily do:

public class User
{
    public int UserId { get; set; }
    public string Email { get; set; }
    public virtual ICollection<Customer> TaggedCustomers { get; set; }
}

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

using(var context = new MyDbContext())
{
    var user = context.Users.Single(o => o.UserId == 0);
    var customers = user.TaggedCustomers;
}

Results in much cleaner code in my opinion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文