如何映射两个一对一关系 [英] How to map two One or Zero to One relationships

查看:184
本文介绍了如何映射两个一对一关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个实体,名称为SellinRequest,MortgageAndRent和Album。每个SellinRequest和MortgageAndRent可能都有一个专辑。

  public class SellingRequest 
{
public int Id {get;组; }
public Album Album {get;组; }
}

public class MortgageAndRent
{
public int Id {get;组; }
public Album Album {get;组; }
}

public class Album
{
public int Id {get;组; }
public SellingRequest SellingRequest {get;组; }
public int SellingRequestId {get;组; }

public MortgageAndRent MortgageAndRent {get;组; }
public int MortgageAndRentId {get;组; }

public List<照片>照片{get;组; }
}

这是我要实现的逻辑:




  • (SellingRequest 1 .... 0-1相册)

  • (MortgageAndRent 1 .... 0-1相册)



使用这些映射:

  public class SellingRequestMap:EntityTypeConfiguration< SellingRequest> 
{
public SellingRequestMap()
{
this.ToTable(SellingRequest);
this.HasKey(sR => sR.Id);

//每个SellingRequest可能有一张专辑。 (SellingRequest 1 .... 0-1 Album)
this.HasOptional(sR => sR.Album).WithOptionalPrincipal(a => a.SellingRequest).WillCascadeOnDelete(false);
}
}

public RentAndMortgageMap()
{
this.ToTable(MortgageAndRent);
this.HasKey(mR => mR.Id);

//每个MortgageAndRent可能有一张专辑。 (MortgageAndRent 1 .... 0-1 Album)
this.HasOptional(sM => sM.Album).WithOptionalPrincipal(a => a.MortgageAndRent).WillCascadeOnDelete(false);
}

但我无法得到结果。我不知道如何将这两张表与Album表相关联。

解决方案

这是我将如何做诡异的语法。请注意新的 id 字段,并将相册属性标记为

  public class SellingRequest 
{
public int Id {get;组; }
public int AlbumId {get; set;}

public virtual Album Album {get;组; }
}

public class MortgageAndRent
{
public int Id {get;组; }
public int AlbumId {get; set;}

public virtual Album Album {get;组; }
}

public class Album
{
public int Id {get;组; }

public int SellingRequestId {get;组; }
public int MortgageAndRentId {get;组; }

public virtual SellingRequest SellingRequest {get;组; }
public virtual MortgageAndRent MortgageAndRent {get;组; }

public virtual List< Photo>照片{get;组; }
}


I have three entities with the name of SellinRequest, MortgageAndRent and Album. Each of SellinRequest and MortgageAndRent may have an Album.

public class SellingRequest
{
    public int Id { get; set; }
    public Album Album { get; set; }  
}

public class MortgageAndRent
{
    public int Id { get; set; } 
    public Album Album { get; set; }
}

public class Album
{
    public int Id { get; set; }
    public SellingRequest SellingRequest { get; set; }
    public int SellingRequestId { get; set; }

    public MortgageAndRent MortgageAndRent { get; set; }
    public int MortgageAndRentId { get; set; }

    public List<Photo> Photos { get; set; }
}

This is the logic I want to be implemented:

  • (SellingRequest 1 .... 0-1 Album)
  • (MortgageAndRent 1 .... 0-1 Album)

With these mappings:

public class SellingRequestMap : EntityTypeConfiguration<SellingRequest>
{
    public SellingRequestMap()
    {
        this.ToTable("SellingRequest");
        this.HasKey(sR => sR.Id);

        // Each SellingRequest may have one Album. (SellingRequest 1 .... 0-1 Album)
        this.HasOptional(sR => sR.Album).WithOptionalPrincipal(a => a.SellingRequest).WillCascadeOnDelete(false);
    }
}

public RentAndMortgageMap()
    {
        this.ToTable("MortgageAndRent");
        this.HasKey(mR=>mR.Id);

        // Each MortgageAndRent may have one Album. (MortgageAndRent 1 .... 0-1 Album)
        this.HasOptional(sM => sM.Album).WithOptionalPrincipal(a => a.MortgageAndRent).WillCascadeOnDelete(false);
    }

But I couldn't get the result. I don't know how to relate these two tables to Album table!

解决方案

Here is how I would do it w/o the fleunt syntax. Note the new id fields and that I marked the album properties as virtual

public class SellingRequest
{
    public int Id { get; set; }
    public int AlbumId {get;set;}

    public virtual Album Album { get; set; }  
}

public class MortgageAndRent
{
    public int Id { get; set; } 
    public int AlbumId {get;set;}

    public virtual Album Album { get; set; }
}

public class Album
{
    public int Id { get; set; }

    public int SellingRequestId { get; set; }   
    public int MortgageAndRentId { get; set; }

    public virtual SellingRequest SellingRequest { get; set; }
    public virtual MortgageAndRent MortgageAndRent { get; set; }

    public virtual List<Photo> Photos { get; set; }
}

这篇关于如何映射两个一对一关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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