Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用DiscriminateSubClassesOnColumn问题的Fluent NHibernate子类映射_C#_Asp.net Mvc_Nhibernate_Fluent Nhibernate - Fatal编程技术网

C# 使用DiscriminateSubClassesOnColumn问题的Fluent NHibernate子类映射

C# 使用DiscriminateSubClassesOnColumn问题的Fluent NHibernate子类映射,c#,asp.net-mvc,nhibernate,fluent-nhibernate,C#,Asp.net Mvc,Nhibernate,Fluent Nhibernate,我有一个映射问题,我的关系看起来像这样。 我有家长课: public abstract class DocumentType { public virtual int Id { get; set; } public virtual string Name { get; set; } } 和两个子类: public class UploadedFileDocument : DocumentType { } public class ApplicationFormDocumen

我有一个映射问题,我的关系看起来像这样。 我有家长课:

public abstract class DocumentType
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}
和两个子类:

public class UploadedFileDocument : DocumentType
{

}

public class ApplicationFormDocument : DocumentType
{
}
映射如下:

public DocumentTypeMap()
{
    Schema("Core");
    Id(x => x.Id);
    Map(x => x.Name).Length(128).Not.Nullable();
    DiscriminateSubClassesOnColumn("Type");
}

public class UploadedFileDocumentMap : SubclassMap<UploadedFileDocument>
{

}

public class ApplicationFormDocumentMap : SubclassMap<ApplicationFormDocument>
{

}
public FileConversionMap()
{
    Schema("Core");
    Id(x => x.Id);
    References(x => x.Application).Not.Nullable();
    References(x => x.DocumentType).Not.Nullable().Fetch.Select();           
}
Session.Query<FileConversion>().AsQueryable();
我的问题是,当我像这样从数据库检索行时:

public DocumentTypeMap()
{
    Schema("Core");
    Id(x => x.Id);
    Map(x => x.Name).Length(128).Not.Nullable();
    DiscriminateSubClassesOnColumn("Type");
}

public class UploadedFileDocumentMap : SubclassMap<UploadedFileDocument>
{

}

public class ApplicationFormDocumentMap : SubclassMap<ApplicationFormDocument>
{

}
public FileConversionMap()
{
    Schema("Core");
    Id(x => x.Id);
    References(x => x.Application).Not.Nullable();
    References(x => x.DocumentType).Not.Nullable().Fetch.Select();           
}
Session.Query<FileConversion>().AsQueryable();
Session.Query().AsQueryable();
返回的所有行的
DocumentType
类型都是
DocumentType
,而不是子类型(即该属性的实际类型,即当我执行
.GetType()
时,
UploadedFileDocument
ApplicationFormDocument


抱歉,如果这只是因为我太迟钝了。但是我怎样才能确定我拥有哪种类型的
DocumentType
。。。我的映射错误吗?

当您查看生成的SQL(将
.ShowSQL()
添加到
.Database
方法中)时,是否看到输入的类型?您应该看到类似的内容:

INSERT 
INTO
    "Core_DocumentType"
    (Name, Type) 
VALUES
    (@p0, 'ApplicationFormDocument');
select
    last_insert_rowid();
@p0 = 'afd' [Type: String (0)]
使用您提供的映射,它看起来很好,我可以很好地返回
DocumentType
(使用SQLite)

这是我用来复制它的代码。我没有您的
FileConversion
对象,因此请验证它是否符合您的需要

文档类型

public class DocumentType
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}

public class DocumentTypeMap : ClassMap<DocumentType>
{
    public DocumentTypeMap()
    {
        GenerateMap();
    }

    void GenerateMap()
    {
        Schema("Core");
        Id(x => x.Id).GeneratedBy.Identity();
        Map(x => x.Name).Length(128).Not.Nullable();
        DiscriminateSubClassesOnColumn("Type");
    }
}
通过对断言进行调试,我可以看到集合返回了两种类型:


是否将它们转换回映射或类中的任何基类型?

当查看生成的SQL(将
.ShowSQL()
添加到
.Database
方法中)时,是否看到输入的类型?您应该看到类似的内容:

INSERT 
INTO
    "Core_DocumentType"
    (Name, Type) 
VALUES
    (@p0, 'ApplicationFormDocument');
select
    last_insert_rowid();
@p0 = 'afd' [Type: String (0)]
使用您提供的映射,它看起来很好,我可以很好地返回
DocumentType
(使用SQLite)

这是我用来复制它的代码。我没有您的
FileConversion
对象,因此请验证它是否符合您的需要

文档类型

public class DocumentType
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}

public class DocumentTypeMap : ClassMap<DocumentType>
{
    public DocumentTypeMap()
    {
        GenerateMap();
    }

    void GenerateMap()
    {
        Schema("Core");
        Id(x => x.Id).GeneratedBy.Identity();
        Map(x => x.Name).Length(128).Not.Nullable();
        DiscriminateSubClassesOnColumn("Type");
    }
}
通过对断言进行调试,我可以看到集合返回了两种类型:


您是否将它们转换回映射或类中的任何基类型?

我最终使用了不同的数据库模式,因此一切都发生了变化,但David的回答非常有用,我非常确定他是对的,因此标记为已接受的答案我最终使用了不同的数据库模式,因此一切都发生了变化,但大卫的回答非常有帮助,我很肯定他是对的,所以打成了公认的答案