-
Notifications
You must be signed in to change notification settings - Fork 2
hasone mapping
Todd Thomson edited this page Aug 19, 2018
·
3 revisions
To configure a 1-1 relationship on an entity property use the HasOne() method.
entity.HasOne( p => p.Supplier )
.WithForeignKey( p => p.SupplierId )
.References<Supplier>( p => p.Id );
In the above example:
- The .HasOne() lambda expression specifies the entity relationship property.
- The .WithForeignKey() lambda expression specifies the entity foreign key property.
- The .References<>() lambda expression specifies the foreign key reference entity and (usually) primary key.
In your entity class use EntityReference<TEntity>
to declare your 1-1 relationship properties.
public class Product
{
/// <summary>
/// Gets or sets the order id. Primary key.
/// </summary>
public int Id { get; set; }
/// <summary>
/// Gets or sets the product name.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets the product price.
/// </summary>
public double Price { get; set; }
/// <summary>
/// Gets or sets the SupplierId. Foreign key. Required.
/// </summary>
public int SupplierId { get; set; }
/// <summary>
/// Gets or sets the supplier entity. HasOne, 1-1 relationship.
/// </summary>
public EntityReference<Supplier> Supplier { get; set; }
/// <summary>
/// Gets or sets the set of product parts. HasMany, 1-many relationship.
/// </summary>
public EntityCollection<Part> Parts { get; set; }
}