【问题标题】:How to map two One or Zero to One relationships如何映射两个一或零到一的关系
【发布时间】:2017-03-12 05:10:34
【问题描述】:

我有三个实体,名称分别为 SellinRequest、MortgageAndRent 和 Album。 SellinRequest 和 MortgageAndRent 中的每一个都可能有一个相册。

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; }
}

这是我要实现的逻辑:

  • (SellingRequest 1 .... 0-1 专辑)
  • (MortgageAndRent 1 .... 0-1 专辑)

使用这些映射:

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);
    }

但我无法得到结果。我不知道如何将这两个表关联到专辑表!

【问题讨论】:

  • 您的相册实体没有定义键。同样在“WithOptionalPrincipal”之后,我会推荐“MapKey”指向外键。
  • 不太清楚你的意思是什么但我无法得到结果
  • @Milos Mijatovic 非常感谢你记得我。在原始代码中,我有 Id,但我忘了在这里写 Id。
  • @MilosMijatovic 请您多解释一下这一行:“在“WithOptionalPrincipal”之后,我建议使用“MapKey”指向外键。” – Atefeh Mohammadpoor

标签: entity-framework ef-code-first entity-framework-6 mapping


【解决方案1】:

以下是我在不使用 fleunt 语法的情况下的做法。请注意新的 id 字段,并且我将专辑属性标记为 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; }
}

【讨论】:

  • 你好@jhilden。感谢您的回答。我已经按照您使用我自己的映射建议的方式更改了代码(上面考虑了映射。)。但我收到此错误:“无法将值 NULL 插入 'Id' 列,表 'RealEstateV1Database1.dbo.Album';列不允许空值。INSERT 失败。\r\n语句已终止。”} 虽然我有把这行代码放到Album Mapping中:this.HasKey(a => a.Id); this.Property(a => a.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  • 请问您现在对映射有什么想法吗?
猜你喜欢
  • 2016-09-20
  • 2012-06-26
  • 2020-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多