C# 如何通过实体框架访问多对多表?asp.net
如何通过EF读取多对多表格?我不知道如何使用多对多表。比如说Product_Category,其中包含ProductID和CategoryID 我怎样才能通过例如C# 如何通过实体框架访问多对多表?asp.net,c#,sql,asp.net,entity-framework,many-to-many,C#,Sql,Asp.net,Entity Framework,Many To Many,如何通过EF读取多对多表格?我不知道如何使用多对多表。比如说Product_Category,其中包含ProductID和CategoryID 我怎样才能通过例如 using(Entities db = new Entities) { /* cant access these here.. */} 方法??但是,我可以访问Product_类别,但无法访问其ProductID或CategoryID 我想列出每种产品,例如,其中product_Category.CategoryID==Ca
using(Entities db = new Entities)
{
/* cant access these here.. */}
方法??但是,我可以访问Product_类别,但无法访问其ProductID或CategoryID
我想列出每种产品,例如,其中product_Category.CategoryID==Category.ID
我以前从未使用过多对多表,因此我很欣赏一些简单的示例,说明如何通过asp.net中的EF访问这些表
谢谢您必须将product和category表与bridge table product_category连接起来,以检索所需的产品信息
using(eShopEntities db = new eShopEntities)
{
var products = (from p in db.Product_Category
join ProductTable pt on p.ID = pt.ProductID
join Category c on c.ID = P.CategoryID
select new
{
p.ID,
p.Name,
p.Description,
p.Price
}).ToList();
}
您必须将product和category表与桥接表product_category连接起来,以检索所需的产品信息
using(eShopEntities db = new eShopEntities)
{
var products = (from p in db.Product_Category
join ProductTable pt on p.ID = pt.ProductID
join Category c on c.ID = P.CategoryID
select new
{
p.ID,
p.Name,
p.Description,
p.Price
}).ToList();
}
导航属性是您在这里的朋友。除非联接表中有其他属性,否则不需要它。这就是为什么您的模型中没有产品类别。因此,假设您的模型是:
public class Product
{
public Product()
{
this.Categories = new HashSet<Category>();
}
public int ProductId { get; set; }
public string ProductName { get; set; }
public virtual ICollection<Category> Categories { get; set; }
}
public class Category
{
public Category()
{
this.Products = new HashSet<Product>();
}
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
如果您确实需要显式连接表,请参见:导航属性是您的朋友。除非联接表中有其他属性,否则不需要它。这就是为什么您的模型中没有产品类别。因此,假设您的模型是:
public class Product
{
public Product()
{
this.Categories = new HashSet<Category>();
}
public int ProductId { get; set; }
public string ProductName { get; set; }
public virtual ICollection<Category> Categories { get; set; }
}
public class Category
{
public Category()
{
this.Products = new HashSet<Product>();
}
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
如果您确实想要一个显式连接表,请参见以下内容:在db中输入var products=from p时,如何访问产品类别的可能重复项的可能重复项。产品类别不会显示在列表中。当我添加桥接表时,edmx设计只显示了product和category之间的新关系,即m-2-m,但product_category类并不像其他表那样自动创建。我必须手动创建Product_Category类吗?如果数据库中存在Product_Category表,那么它也应该显示在edmx设计中。我将Product_Category中的两个ID都添加为外键,而没有任何主键,这会导致这些表之间的多对多关系像edmx设计中的[P-table]*-*[C-tabel]一样,但没有显示产品类别表。我做得对吗?或者我应该也看到产品类别表吗?不,这是EF在幕后处理的。默认情况下,您没有连接表的数据库集。在数据库中输入var products=from p时,如何访问Product_类别。Product_类别不会显示在列表中。当我添加桥接表时,edmx设计只显示了product和category之间的新关系,即m-2-m,但product_category类并不像其他表那样自动创建。我必须手动创建Product_Category类吗?如果数据库中存在Product_Category表,那么它也应该显示在edmx设计中。我将Product_Category中的两个ID都添加为外键,而没有任何主键,这会导致这些表之间的多对多关系像edmx设计中的[P-table]*-*[C-tabel]一样,但没有显示产品类别表。我做得对吗?或者我应该也看到产品类别表吗?不,这是EF在幕后处理的。默认情况下,您没有用于连接表的DbSet。谢谢,这正是我需要的!我想我必须做一些复杂的动作,比如添加另一个类,比如Product_Category,结果真是一团糟。。这比我想象的容易多了。谢谢你,这正是我需要的!我想我必须做一些复杂的动作,比如添加另一个类,比如Product_Category,结果真是一团糟。。这比我想象的要容易得多。