Entity Framework 4 - TPH Inheritance in Features CTP5 (code first) with “IS NULL” discriminator

谁说胖子不能爱 提交于 2019-12-31 04:57:05

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/5019717/entity-framework-4-tph-inheritance-in-features-ctp5-code-first-with-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!