简体   繁体   English

EF PK FK映射没有标量PK属性

[英]EF PK FK mapping without scalar PK properties

Is it posible to define a compound foreign primary key without defining scalar properties? 是否可以定义复合外键而不定义标量属性?

public class A 
{
  public virtual int AID { get; set; }
}

public class B
{
  public virtual int BID { get; set; }
}

public class CompoundKeyClass
{
  public virtual A AObject { get; set; }
  public virtual B BObject { get; set; }
}

How can I make properties AObject and BObject the compound PK of CompoundKeyClass without defining the scalar ID properties of for A and B types using the FluentAPI? 如何在不使用FluentAPI定义A和B类型的标量ID属性的情况下,使属性AObject和BObject成为CompoundKeyClass的复合PK?

You can't. 你不能。 Both AID and BID must be in your CompoundKeyClass mapped as complex primary key and each of them must be mapped as foreign key to related table. AIDBID必须在您的CompoundKeyClass映射为复杂主键,并且每个都必须作为外键映射到相关表。

I suspect you can't; 我怀疑你不能; the closest I can find to documentation on this is this : 我可以找到这个文件的最接近的是这个

KeyAttribute KeyAttribute

KeyAttribute is used to specify that a property/column is part of the primary key of the entity and applies to scalar properties only. KeyAttribute用于指定属性/列是实体主键的一部分,仅适用于标量属性。

You should, however, be able to name the foreign key properties "_AID" and "_BID", and/or mark them as [Browsable(false)] . 但是,您应该能够将外键属性命名为“_AID”和“_BID”,和/或将它们标记为[Browsable(false)] This should ensure you cannot use or even see them accidentally. 这应该确保您不会意外使用甚至看不到它们。

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

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