【发布时间】: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