简体   繁体   English

EF Core 5.0 多对多关系,无需显式映射 - 如何连接两个表?

[英]EF Core 5.0 many-to-many relationships without explicitly mapping - How to join two tables?

As you know, in EF Core 5.0 you can have relationships without explicitly mapping the tables .如您所知,在 EF Core 5.0 中,您无需显式映射表即可拥有关系。 The question is: since you don't have the mapping classes anymore, how can you actually join/filter them?问题是:既然你没有映射类了,你怎么能真正加入/过滤它们?

Take the snipped as example:以截图为例:

public class Product
{
    ...
    public IList<Category> Categories {get;set;}
}

public class Category
{
    ...
    public IList<Product> Products {get;set;}
}

This code in EF 5.0 is (correctly) generating a mapping table named ProductCategory automatically with the proper FK in the database, linking ProductId and CategoryId together. EF 5.0 中的此代码(正确)使用数据库中的正确 FK 自动生成名为 ProductCategory 的映射表,将 ProductId 和 CategoryId 链接在一起。 This table mapping has no equivalent class in the source code.这个表映射在源代码中没有等效的类。

Now my problem is, how can I filter values for those two tables without the mapping class in the code?现在我的问题是,如何在代码中没有映射类的情况下过滤这两个表的值? For example, how to represent the following hypothetical query:例如,如何表示以下假设查询:

SELECT ALL PRODUCTS JOIN CATEGORIES WHERE [HOW TO LINK THEM?] PRODUCT ISAVAILABLE AND CATEGORY BELONGS TO ELETROCNICS选择所有产品加入类别 [如何链接它们?] 产品可用且类别属于 ELECTRONICS

Thanks in advance.提前致谢。

I hope my telepathy is good enough.我希望我的心灵感应足够好。

var query = 
   from p in ctx.Products
   where p.Categories.Any(c => c.Name == "Electorics")
   select p;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM