ReferenceCollectionBuilder<TPrincipalEntity,TDependentEntity>.HasForeignKey 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| HasForeignKey(Expression<Func<TDependentEntity,Object>>) |
配置屬性作為此關係的外鍵。 |
| HasForeignKey(String[]) |
配置屬性作為此關係的外鍵。 |
HasForeignKey(Expression<Func<TDependentEntity,Object>>)
配置屬性作為此關係的外鍵。
public virtual Microsoft.EntityFrameworkCore.Metadata.Builders.ReferenceCollectionBuilder<TPrincipalEntity,TDependentEntity> HasForeignKey(System.Linq.Expressions.Expression<Func<TDependentEntity,object>> foreignKeyExpression);
public virtual Microsoft.EntityFrameworkCore.Metadata.Builders.ReferenceCollectionBuilder<TPrincipalEntity,TDependentEntity> HasForeignKey(System.Linq.Expressions.Expression<Func<TDependentEntity,object?>> foreignKeyExpression);
override this.HasForeignKey : System.Linq.Expressions.Expression<Func<'DependentEntity, obj>> -> Microsoft.EntityFrameworkCore.Metadata.Builders.ReferenceCollectionBuilder<'PrincipalEntity, 'DependentEntity (requires 'PrincipalEntity : null and 'DependentEntity : null)>
Public Overridable Function HasForeignKey (foreignKeyExpression As Expression(Of Func(Of TDependentEntity, Object))) As ReferenceCollectionBuilder(Of TPrincipalEntity, TDependentEntity)
參數
- foreignKeyExpression
- Expression<Func<TDependentEntity,Object>>
一個代表外鍵屬性的 lambda 表達式(post => post.BlogId)。
如果外鍵由多個屬性組成,則指定一個包含屬性(comment => new { comment.BlogId, comment.PostTitle })的匿名類型。 指定的順序應與中 HasPrincipalKey(Expression<Func<TPrincipalEntity,Object>>)對應屬性的順序相符。
傳回
同一個建構器實例,這樣可以串接多個設定呼叫。
備註
若 HasPrincipalKey(Expression<Func<TPrincipalEntity,Object>>) 未指定,則會嘗試將外鍵屬性的資料型態與順序與主實體類型的主鍵相匹配。 若不匹配,則會新增形成唯一索引的影子狀態屬性,作為參考鍵。 影子狀態屬性是指在實體類別中沒有對應屬性的屬性。 屬性的當前值會儲存在實體 ChangeTracker 類別的實例中,而非儲存在實體類別的實例中。
適用於
HasForeignKey(String[])
配置屬性作為此關係的外鍵。
public virtual Microsoft.EntityFrameworkCore.Metadata.Builders.ReferenceCollectionBuilder<TPrincipalEntity,TDependentEntity> HasForeignKey(params string[] foreignKeyPropertyNames);
override this.HasForeignKey : string[] -> Microsoft.EntityFrameworkCore.Metadata.Builders.ReferenceCollectionBuilder<'PrincipalEntity, 'DependentEntity (requires 'PrincipalEntity : null and 'DependentEntity : null)>
Public Overridable Function HasForeignKey (ParamArray foreignKeyPropertyNames As String()) As ReferenceCollectionBuilder(Of TPrincipalEntity, TDependentEntity)
參數
- foreignKeyPropertyNames
- String[]
外鍵屬性的名稱。
傳回
同一個建構器實例,這樣可以串接多個設定呼叫。
備註
若指定的屬性名稱不存在於實體類型中,則會新增一個影子狀態屬性作為外鍵。 影子狀態屬性是指在實體類別中沒有對應屬性的屬性。 屬性的當前值會儲存在實體 ChangeTracker 類別的實例中,而非儲存在實體類別的實例中。
若 HasPrincipalKey(Expression<Func<TPrincipalEntity,Object>>) 未指定,則會嘗試將外鍵屬性的資料型態與順序與主實體類型的主鍵相匹配。 若不匹配,則會新增形成唯一索引的影子狀態屬性,作為參考鍵。