IT:AD:EF/CodeFirst/FluentAPI/Relationships/Examples/1-1 (One-To-One, Required)
Process
//////1-0..1:
modelBuilder.Entity<Invoice>()
.HasRequired(i => i.ShippingAddress)
.WithMany()
.WillCascadeOnDelete(false); //1-0: as it can be shared with User, don't bind Invoice and Address, so avoid .WithRequired
Working with:
public class Address
{
public virtual int Id { get; set; }
public virtual string Street { get; set; }
//...
}
//Requirements to enable the use of change-tracking POCO Proxies:
//* Class must be public, non-abstract, non-sealed.
//* Public *virtual* get/set for all persisted properties.
//* Collection based Navigation properties must implement ICollection<T>
//Ref: http://bit.ly/KgJPcU
public class User
{
public virtual int Id { get; private set; }
//Marking *scalar* as virtual, marks it for Proxy change tracking.
public virtual string FirstName { get; set; }
//1-1 Relationship:
public virtual int BillingAddressId { get; set; }
public virtual Address BillingAddress { get; set; }
//...
//Marking a *Navigation Collection* as virtual, marks it for Lazy Loading
public virtual ICollection<Invoice> Invoices { get { return _invoices??(_invoices = new Collection<Invoice>()); } private set { _invoices = value; }}
private ICollection<Invoice> _invoices;
}