Hey guys, I'm trying to create a TPH mapping on a hierarchy where the discriminating clause is the classical "IS NOT NULL" / "IS NULL" case.

Here is the example, database wise:

CREATE TABLE info.EducationTypes
(
   ID INT NOT NULL PRIMARY KEY,
   Name NVARCHAR(64) NOT NULL,
   FKParentID INT NULL REFERENCES info.EducationTypes(ID)
)

the idea is to have a class hierarchy like the following one:

public abstract class EducationType
{
    public int ID { get; set; }
    public string Name { get; set; }
}

public class MainEducationType : EducationType
{
    public IEnumerable<SubEducationType> SubTypes { get; set; }
}

public class SubEducationType : EducationType
{
    public MainEducationType MainType { get; set; }
}

I got this schema "working" in the classic xml model, but I really can't find a way to get it working by using the code first approach. This is what I tried...

var educationType = modelBuilder.Entity<EducationType>();
educationType.Map<MainEducationType>(m => m.Requires("FKParentID").HasValue(null));
educationType.Map<SubEducationType>(m => m.Requires("FKParentID"));

Do you have any suggestion?

有帮助吗?

解决方案

Unfortunately, having a null value for the discriminator column in TPH mapping is not currently supported in CTP5. This is confirmed by EF team on here and also here. They are looking at it to see if they can make it work for the RTM though.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top