1

Update: I should have noted that the Id property on NavigationPropertyClass is not database generated. In my actual model, NavigationPropertyClass is also part of a hierarchy scheme (using TPH) which is not shown here , and while NavigationPropertyClass has a DerivedClass1 instance and a collection of DerivedClass2 instances, this is not necessarily the case with all classes that inherit from the base class from which NavigationPropertyClass is derived.


A portion of my model looks something like this (other properties and constructors omitted for brevity):

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

DerivedClass1 has a 1-1 relationship with NavigationPropertyClass, while DerivedClass2 has a many-1 relationship with NavigationPropertyClass.

I'm trying to set up identifying relationships in each of the derived classes so that when the instance of NavigationPropertyClass is deleted from the database, so too will the associated instance of DerivedClass1 and any instances of DerivedClass2. The only way I can see to set this up is with TPT inheritance, but even so, I cannot get things working correctly. I'd post my Fluent API configuration, but I've tried so many permutations at this point that I wouldn't know which one to post.

Is there any way to do what I'm trying to do? If so, what does the Fluent API config look like?

4

1 回答 1

0

这是符合您要求的模型。

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也是 FK的部分Derived1

结果

结果

简化生成的表约束NavigationPropertyClass

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

更多的

于 2014-09-03T15:49:46.310 回答