本文介绍了如何使用Fluent NHibernate ReferencesAny映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多有关Fluent NHibernate的ReferencesAny的信息,但是我还没有看到完整的示例.我想我大部分都理解,但是有一部分我不了解.在类映射中,ReferencesAny(x => x.MemberName)用于定义与一个或多个引用类的关系.什么是MemberName?它是如何定义的以及如何用于在数据库中创建数据.

I've read a lot about Fluent NHibernate's ReferencesAny but I haven't seen a complete example. I think I understand most of it, but there is one part I don't get. In the class mapping ReferencesAny(x => x.MemberName) is used to define the relationship to the one or more referenced classes. What is MemberName? How is it defined and how is it used to create the data in the database.

我有三个表,一个表中的记录可以引用其他两个表之一中的记录.前两个是自动映射的,因此没有专门定义Id字段.

I have three tables, the records in one table can reference records in one of the other two tables. The first two are auto mapped, so the Id field is not specifically defined.

public class Household
{
    public virtual string Name { get; set; }

    public virtual IList<AddressXref> AddressXrefs { get; set; }
}

public class Client
{
    public virtual string Name { get; set; }

    public virtual IList<AddressXref> AddressXrefs { get; set; }
}

我不确定AddressXref表是否可以自动映射.如果是这样,我也需要找出方法.现在,我将以Fluent的常规方式进行操作.

I'm not sure if the AddressXref table can be auto mapped. If so I need to find out how to do that too. For now I'll do it the conventional way with Fluent.

public class AddressXref
{
    public virtual int id { get; set; }
    public virtual string TableName { get; set; }
    public virtual Int32 Table_id { get; set; }
    public virtual string Street { get; set; }
    public virtual string City { get; set; }
}

class AddressXrefMap : ClassMap<AddressXref>
{
    public AddressXrefMap()
    {
        Table("AddressXref");
        Id(x => x.id);
        Map(x => x.TableName);
        Map(x => x.Table_id);
        Map(x => x.Street);
        Map(x => x.City);

        ReferencesAny(x => x.TableRef)
            .AddMetaValue<Household>(typeof(Household).Name)
            .AddMetaValue<Client>(typeof(Client).Name)
            .EntityTypeColumn("TableName")
            .EntityIdentifierColumn("Table_id")
            .IdentityType<int>();
    }
}

我需要帮助的部分是如何在类中定义TableRef成员中的TableRef(在ReferencesAny()中引用)?

The part I need help with is how is the TableRef, referred to in ReferencesAny(), member of AddressXref defined in the class?

此外,在创建数据记录时在代码中如何使用它?我认为它会与此相似:

Also, how it is used in the code when creating data records? I image it will be similar to this:

Household Household = new Household();
Household.Name      = "Household #1";

AddressXref AddrXref = new AddressXref();
AddrXref.Street1   = "123 Popular Street";
AddrXref.City      = "MyTown";
AddrXref.TableRef  = Household;

Session.SaveOrUpdate(AddrXref);

我喜欢将Fluent与NHibernate结合使用,但是我仍然对学习曲线感到惊讶. :)

I love using Fluent with NHibernate, but I'm still amazed at the learning curve. :)

谢谢,拉斯

推荐答案

由于Household和Client除了对象都不共享基类,因此必须将其声明为:

since both Household and Client don't share a base class other than object you have to declare it as this:

public class AddressXref
{
    public virtual int Id { get; set; }
    public virtual object TableRef { get; set; }
    public virtual string Street { get; set; }
    public virtual string City { get; set; }
}

并像这样测试它

if (addrXref.TableRef is HouseHold)
    // it's a household

这篇关于如何使用Fluent NHibernate ReferencesAny映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 10:33