C# 如何在实体框架中为外键指定表?

C# 如何在实体框架中为外键指定表?,c#,entity-framework-6,C#,Entity Framework 6,我读过的教程中的所有示例都只显示了两个表,所以我不知道在下面的场景中该怎么做 假设我们有3个班: public class Product { public int id { get; set; } public string name { get; set; } public string skuA { get; set; } public string skuB { get; set; } public

我读过的教程中的所有示例都只显示了两个表,所以我不知道在下面的场景中该怎么做

假设我们有3个班:

public class Product
    {
        public int id { get; set; }
        public string name { get; set; }
        public string skuA { get; set; }
        public string skuB { get; set; }

        public virtual SupplierA supplierA { get; set; }
        public virtual SupplierB supplierB { get; set; }
    }
public class Supplier A
    {
        public int id { get; set; }
        public string name { get; set; }
        public string sku { get; set; }
        public int price { get; set; }
    }
public class Supplier B
    {
        public int id { get; set; }
        public string name { get; set; }
        public string sku { get; set; }
        public int price { get; set; }
    }
如果我们需要加入适当的产品sku字段,那么使用我在教程中看到的数据注释,结果如下所示:

[ForeignKey("sku")]
public string skuA { get; set; }
[ForeignKey("sku")]
public string skuB { get; set; }
实体框架如何知道我们为哪个外来“sku”字段进行注释?每个外键的命名是否必须不同?我想如果我们有100家供应商,这会很尴尬


是否有其他参数或标记来处理此问题?任何建议都值得赞赏,因为我发现这令人困惑。

我误解了外键属性的用途。若不遵循命名约定,则在当前类上定义键


我被卡住了,因为收到的错误消息说我需要使用注释或fluent api,但它真正的意思是我必须使用fluent api,因为我想要的关系不能用注释指定。

只是想理解一下,为什么不将两个供应商都存储在“供应商”下?而不是有一个,B。。。n@Harry本表为供应商产品,非供应商记录。为了与目录数据保持一致,字段可能因供应商而异(如果有意义的话)。sku代表一个项目的单个供应商代码,可能因供应商而异,因此如果您理解我的意思,产品表是将它们连接在一起的粘合剂。感谢您的澄清