通过流畅API第一实施零个或一个为零,或在EF code一一对应关系 [英] Implementing Zero Or One to Zero Or One relationship in EF Code first by Fluent API

查看:280
本文介绍了通过流畅API第一实施零个或一个为零,或在EF code一一对应关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个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; }
  ....   
}

每个订单可以从一个或零报价制成,每个报价的可以作出的命令,所以我有一个一零到一零的关系,我怎么能实现这个在EF code首先通过流畅API?

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?

推荐答案

通过改变波苏斯为:

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):

特别感谢(瓦希德先生纳西

这篇关于通过流畅API第一实施零个或一个为零,或在EF code一一对应关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆