簡體   English   中英

實體框架6.1:1-1和1-多派生類型中的關系和標識關系

[英]Entity framework 6.1: 1-1 and 1-many relationship in derived types and identifying relationships

更新:我應該注意到NavigationPropertyClassId屬性不是數據庫生成的。 在我的實際模型中, NavigationPropertyClass也是層次結構方案的一部分(使用TPH),此處未顯示,盡管NavigationPropertyClass具有DerivedClass1實例和DerivedClass1實例的集合, DerivedClass2繼承的所有類都不一定是這種情況從其派生NavigationPropertyClass基類。


我的模型的一部分看起來像這樣(為簡潔起見,省略了其他屬性和構造函數):

//Many other classes inherit from this class
//and many of the derived classes are not composed of DerivedClass1 or DerivedClass2
public abstract SomeOtherBaseClass{
    public int Id {get; set;} //not database generated, set in the constructor
}

public NavigationPropertyClass: SomeOtherBaseClass{
    public DerivedClass1 Derived1 {get; set;}
    public virtual ICollection<DerivedClass2> Derived2Collection {get; set;}
}

public abstract class BaseClass{
    public int Id {get; set;}
    //other properties shared by derived classes
}

public class DerivedClass1: BaseClass{
    public NavigationPropertyClass NavigationProperty {get; set;}
    //other properties pertinent to DerivedClass1
}


public class DerivedClass2: BaseClass{
    public NavigationPropertyClass NavigationProperty {get; set;}
    //other properties pertinent to DerivedClass2
}

DerivedClass1NavigationPropertyClass關系為1-1,而DerivedClass2NavigationPropertyClass關系為-1。

我試圖建立識別每一個派生類的關系,這樣當實例NavigationPropertyClass從數據庫中刪除,因此也將在相關實例DerivedClass1和任何情況下DerivedClass2 我可以看到的唯一方法是使用TPT繼承,但是即使這樣,我也無法正常工作。 我會發布我的Fluent API配置,但是此時我嘗試了很多排列,以至於我不知道要發布哪個。

有什么辦法可以做我想做的事嗎? 如果是這樣,Fluent API配置是什么樣的?

這是符合您要求的模型。

public abstract class Base
{
    public int Id { get; set; }
}
public class Derived1 : Base
{
    public int PropDerived1 { get; set; }
    public NavigationPropertyClass NavigationProperty { get; set; }
}
public class Derived2 : Base
{
    public int PropDerived2 { get; set; }

    public int NavigationPropertyClassId { get; set; }
    public NavigationPropertyClass NavigationPropertyClass { get; set; }
}
public abstract class SomeOtherBaseClass
{
    public int Id { get; set; }
}
public class NavigationPropertyClass : SomeOtherBaseClass
{
    public Derived1 Derived1 { get; set; }
    public virtual ICollection<Derived2> Derived2s { get; set; }
}

而且,在配置模型構建器時,只需要使用ToTable即可繼承TPT。

public class AppContext : DbContext
{
    public DbSet<SomeOtherBaseClass> SomeOtherBaseClasses { get; set; }
    public DbSet<Base> Bases { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Derived1>().ToTable("Derived1");
        modelBuilder.Entity<Derived2>().ToTable("Derived2");
        modelBuilder.Entity<NavigationPropertyClass>().ToTable("NavigationPropertyClass");

        modelBuilder.Entity<NavigationPropertyClass>()
            .HasRequired(x => x.Derived1)
            .WithRequiredDependent(x => x.NavigationProperty);
    }
}

並將所有內容保留為EF約定 ,除了NavigationPropertyClass::Id也是Derived1的FK的Derived1

結果

結果

NavigationPropertyClass表上的簡化生成約束:

PRIMARY KEY [Id]
FOREIGN KEY([Id]) REFERENCES [dbo].[Derived1] ([Id])
FOREIGN KEY([Id]) REFERENCES [dbo].[SomeOtherBaseClasses] ([Id])

更多

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM