实体框架1-0..1与继承基类的关系

时间:2017-12-19 21:20:58

标签: c# entity-framework

我有一些继承自基类和1-0..1关系的类,这里是基类:

public class Base
{
    public int ID { get; set; }
    public byte Status { get; set; }
    public bool IsActive { get; set; }
    ...
}

我继承的课程是:

public class Resim : Base
{
    public string Resim_Orjinal_Ad { get; set; }
    public string Ad_Guid { get; set; }
    public string SEOAlt { get; set; }
    public byte Tip { get; set; }
    public byte Konum { get; set; }

    public virtual Varyasyon Varyasyon { get; set; }
}

public class Varyasyon : Base
{
    public string VaryasyonAdi { get; set; }
    public string VaryasyonOzellik { get; set; }
    public string VaryasyonDeger { get; set; }

    public virtual Resim Resim { get; set; }
}

在上下文中,我想构建一个这样的关系:

 modelBuilder.Entity<Varyasyon>().HasOptional(x => x.Resim).WithRequired(y => y.Varyasyon);

在包管理器控制台中使用add-migration命令时会发生错误:

 Varyasyon_Resim_Target: : Multiplicity is not valid in Role 'Varyasyon_Resim_Target' in relationship 'Varyasyon_Resim'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

身份ID密钥在Base类中,因此当我使用类似
的注释时     [Key,ForeignKey(“Varyasyon”)]在继承的类中,数据库结构被破坏。

我找到了这样的解决方案:

  public class Resim : Base
{
    ...
    public int BaglantiID { get; set; }       
    public virtual ICollection<Varyasyon> Varyasyon { get; set; }
}

public class Varyasyon : Base
{
    ...
    public virtual ICollection<Resim> Resim { get; set; }
}

在上下文中:

 modelBuilder.Entity<Varyasyon>().HasMany(x => x.Resim).WithRequired(y => y.Varyasyon).HasForeignKey(y => y.BaglantiID);

但是这个解决方案不是1-0..1关系,我怎样才能在1-0..1关系中解决这个问题?

0 个答案:

没有答案