English 中文(简体)
c# 流利的一对一关系一对许多关系物体返回无效
原标题:c# Fluent NHibernate one-to-many relationship object returning null

真的想让我的脑袋绕过这里,却看不到树上的光线。 可能错过了明显的东西!!

当我在数据库中创建对象时 它在任何问题中起作用 但当我试图列出值时 链接是无效的...

有两个对象,即 question Response 。一个问题可以有许多答复,因此,以下是模型:

public class Question
{
    public int Id { get; set; }
    public int QuestionNumber { get; set; }
    public string QuestionText { get; set; }
    public IList<Response> Responses { get; set; } 

    public Question()
    {
        Responses = new List<Response>();
    }
}

public class Response
{
    public int Id { get; private set; }
    public Question Question { get; set; }
    public int ResponseValue { get; set; }
    public string ResponseText { get; set; }
}

以下是地图:

public class QuestionMap : ClassMap<Question>
{
    public QuestionMap()
    {
        Id(q => q.Id);
        Map(q => q.QuestionNumber);
        Map(q => q.QuestionText).Length(300).Not.Nullable();
        HasMany<Response>(q => q.Responses).Inverse().AsBag();
    }
}

public class ResponseMap : ClassMap<Response>
{
    public ResponseMap()
    {
        Id(x => x.Id);
        References<Question>(x => x.Question).Not.Nullable();
        Map(x => x.ResponseText);
        Map(x => x.ResponseValue);
    }
}

所以当我使用

var responses = _session.CreateCriteria(typeof(Response)).List<Response>();

并调试和查看问题对象,它总是无效的。

最佳回答

默认情况下, Nhebertate 中的所有关系都是懒惰的。 所以您有两个选项 。 设置为“ 不 ” 。 Lazy Load () 或“ 获取策略”, 或“ 获取策略”, 在您对响应的映射中, 或“ 在查询数据库时”, 或“ 获取策略 ” 。

var responses = _session.CreateCriteria(typeof (Response))
                .SetFetchMode("Responses", NHibernate.FetchMode.Eager)
                .List();

使用.SetFetFetchMode (...) 时,您可能需要在对响应的映射中设置抓取策略。 与这两个设置一起玩玩- 一次一个或两个

PS:顺便说一下,我感到困惑的是,为什么没有.lazyload and fatchs is working and a question to that. FluentHebertnate:Lazy Looad和Phack

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签