.net 如何在SQL Server上映射属于同一个表的两条记录?

.net 如何在SQL Server上映射属于同一个表的两条记录?,.net,sql-server,entity-framework,.net,Sql Server,Entity Framework,我使用的是Sql Server、EF和MVC 我有几张像这样的桌子 Product Id (int,PK) Supplier (int,FK) ProductName (varchar) 我想映射多个产品,以便在查询产品时可以找到其他供应商的等效产品 我正在尝试为地图添加另一个表 ProductMap ProductId (int) Supplier (int) OtherProductId (int

我使用的是Sql Server、EF和MVC 我有几张像这样的桌子

 Product
  Id          (int,PK)
  Supplier    (int,FK)
  ProductName (varchar)
我想映射多个产品,以便在查询产品时可以找到其他供应商的等效产品

我正在尝试为地图添加另一个表

  ProductMap
     ProductId      (int)
     Supplier       (int)
     OtherProductId (int)

但这种方式很难质疑。那么有没有更好的方法呢?

你能不能这样查询一下呢

SELECT  p.Id ,
        p.Supplier ,
        p.ProductName ,
        pm.Supplier ,
        pm.OtherProductId
FROM    Product p
        INNER JOIN ProductMap pm ON p.ID = pm.ProductId

你的问题很模糊,但我想我知道你想要什么

如果您的“产品”是相同的东西,可以从多个供应商处购买,那么您只需要存储每个产品一次。作为一个类,它如下所示:

class Product
{
    public int Id { get; set; }
    public string ProductName { get; set; }
    public List<Supplier> Suppliers { get; set; }
}
Product                    
    ProductId       (int)
    ProductName     (varchar)

ProductSupplier
    ProductId       (int)
    SupplierId      (int)

Supplier
    SupplierId      (int)
    SupplierName    (varchar)
class Product
{
    public int Id { get; set; }
    public string ProductName { get; set; }
    public Supplier Supplier { get; set; }
    public Category Category { get; set; }
}
Product                    
    ProductId       (int)
    ProductName     (varchar)
    CategoryId      (int)
    SupplierId      (int)

ProductCategory
    Id              (int)
    Description     (varchar)

Supplier
    SupplierId      (int)
现在,从产品到拥有该产品的供应商之间存在一对多关系,因为您使用的是实体框架,所以您的查询在C#中是这样的:

这会给你一份提供该产品的所有人的名单。如果不同的供应商有不同的价格,那么只需在
ProductSupplier
表中添加一个
Price
列,以存储此供应商销售此产品的价格

如果你的“产品”不是100%完全相同,并且你正在寻找其他人销售的类似产品,那么你想要一个类似的产品,但是你有产品“类别”(或“类型”或你想称之为它们的任何东西),你用它来存储被认为是等价的东西。从那里,您的
产品
将有一个
ProductCategoryId
外键,因此对于给定的产品,您可以查找其类别以查找所有等效项。作为一个类,它如下所示:

class Product
{
    public int Id { get; set; }
    public string ProductName { get; set; }
    public List<Supplier> Suppliers { get; set; }
}
Product                    
    ProductId       (int)
    ProductName     (varchar)

ProductSupplier
    ProductId       (int)
    SupplierId      (int)

Supplier
    SupplierId      (int)
    SupplierName    (varchar)
class Product
{
    public int Id { get; set; }
    public string ProductName { get; set; }
    public Supplier Supplier { get; set; }
    public Category Category { get; set; }
}
Product                    
    ProductId       (int)
    ProductName     (varchar)
    CategoryId      (int)
    SupplierId      (int)

ProductCategory
    Id              (int)
    Description     (varchar)

Supplier
    SupplierId      (int)
作为SQL表,它将如下所示:

class Product
{
    public int Id { get; set; }
    public string ProductName { get; set; }
    public List<Supplier> Suppliers { get; set; }
}
Product                    
    ProductId       (int)
    ProductName     (varchar)

ProductSupplier
    ProductId       (int)
    SupplierId      (int)

Supplier
    SupplierId      (int)
    SupplierName    (varchar)
class Product
{
    public int Id { get; set; }
    public string ProductName { get; set; }
    public Supplier Supplier { get; set; }
    public Category Category { get; set; }
}
Product                    
    ProductId       (int)
    ProductName     (varchar)
    CategoryId      (int)
    SupplierId      (int)

ProductCategory
    Id              (int)
    Description     (varchar)

Supplier
    SupplierId      (int)
同样,EF使查询非常简单:

var similarProducts = myDbContext.Products
                    .Single(p => p.ProductName == product")
                    .Category.Products.ToList();

你的问题缺少一个非常重要的部分,没有足够的细节来猜测答案。很抱歉,这里有三个产品和两个供应商
P1=Apple,
P2=Banana,
P3=Orange
S1销售(id=1-Apple,id=3-Banana)
S2销售(id=2-Apple,id=4-Banana,Orange)

我想将每个供应商的苹果映射到每个苹果
,这样我就可以看到总数量,按苹果种类销售外部产品