在Fluent API中首先实现“零”或“一到零”或“一个关系” [英] Implementing Zero Or One to Zero Or One relationship in EF Code first by Fluent API
本文介绍了在Fluent API中首先实现“零”或“一到零”或“一个关系”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个POCO类
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; }
....
}
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; }
....
}
每个订单可以从一个或零报价,并且每个报价可能会导致订单,所以我有一个或一个一个或零关系,我如何在流动的API中首先在EF代码中实现这一点?
each Order may made from one or zero quotation, and each quotation may cause an order, so I have an "one or zero" to "one or zero" relation, how can I implement this, in EF Code first by fluent API?
推荐答案
通过将pocos更改为:
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; }
}
并使用这些映射文件:
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"));
}
}
我们将有这个DB(这意味着0 .. 1-0..1):
we will have this DB(that means 0..1-0..1):
特别感谢( Vahid Nasiri先生)
这篇关于在Fluent API中首先实现“零”或“一到零”或“一个关系”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文