I have two POCO classes:
Order Class:
public class Order
{
int id;
string code;
int? quotationId; //it is foreign key
public int Id{get;set;}
public string Code{get;set;}
public int? QuotationId{get;set;}
Quotation quotation;
public virtual Quotation Quotation { get; set; }
....
}
Quotation Class:
public class Quotation
{
int Id;
string Code;
public int Id{get;set;}
public string Code{get;set;}
Order order;
public virtual Order Order { get; set; }
....
}
Order
may be made from one or zero quotation, andSo I have an "one or zero" to "one or zero" relation, how can I implement this, in EF
Code first by Fluent
API?
By changing pocos to:
public class Order
{
public int OrderId { get; set; }
public virtual Quotation Quotation { get; set; }
}
public class Quotation
{
public int QuotationId { get; set; }
public virtual Order Order { get; set; }
}
and using these mapping files:
public class OrderMap : EntityTypeConfiguration<Order>
{
public OrderMap()
{
this.HasOptional(x => x.Quotation)
.WithOptionalPrincipal()
.Map(x => x.MapKey("OrderId"));
}
}
public class QuotationMap : EntityTypeConfiguration<Quotation>
{
public QuotationMap()
{
this.HasOptional(x => x.Order)
.WithOptionalPrincipal()
.Map(x => x.MapKey("QuotationId"));
}
}
we will have this DB(that means 0..1-0..1):
with special thanks to (Mr. Vahid Nasiri)