English 中文(简体)
2. 液化NHibernate和多个关键栏目
原标题:Fluent NHibernate and multiple key columns

I have a User entity that has a IList property. The mapping for this looks like this:

        HasMany(x => x.Forms) 
            .Cascade.None() 
            .AsBag().KeyColumn("Supervisor") 
            .Key(key => key.Not.Update()) 
            .PropertyRef("Email"); 

Now I have a new feature request in that essentially adds another KeyColumn that should ALSO populate into this property. Essentially, each form can have a "Supervisor" and an "Alternate", and I need this property to populate any Forms where the User.Email is equal to either one (Forms BELONG to a User IF the User.Email == Form.Supervisor OR User.Email == Form.Alternate).

Is there a way to do this? Simply adding another KeyColumn specification simply overrides the previous one, and adding keys via the Keys() method seems to not allow for multiple key columns like I want... I m looking for a way to essentially tell it that this relationship should be found with a WHERE Superviser = Email OR Alternate = Email, but it doesn t seem to support that OR clause...

我的另一种选择基本上是重复这一绘图,但对于在另一个财产中“兄弟”的描述,然后用我消费的法典将收集材料合在一起,但我希望看到,NHibernate是否有足够的智慧这样做。

Any suggestions?

最佳回答

据我所知,NHibernate不支持这种不寻常的加入形式。 我与你一样对待这一点。 使用两种收集方法,即<代码>User.SupervisorForms和User.AlternateForms,然后将其合并起来,或许使用LINQ s代码<>Concat。 您可以确定一种财产,用于对你进行这种分类:

public virtual IEnumerable<Form> Forms
{
    get { return SupervisorForms.Concat(AlternateForms); }
}

如果你希望把一个<代码>User填入一个完整初始的<代码>Forms 收集单轮到数据库,那么你可使用NHibernate文件:

User user = s.CreateMultiQuery()
    .Add("select u from User u left join fetch u.SupervisorForms where u.Id = :id")
    .Add("select u from User u left join fetch u.AlternateForms where u.Id = :id")
    .SetInt32("id", 123)
    .UniqueResult<User>();

该代码使用<条码>CreateMulti Query()和《总部协定》——但该方法应当与你选择的盘点工作同时进行:CreateMulti Query(,CreateMultiCriteria(,或Future(/code>)。

问题回答

暂无回答




相关问题
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. ...

热门标签