找到你要的答案

Q:Fluent NHibernate HASMANY mapping without references

Q:流利NHibernate有没有参考图

I am a beginner at using Fluent NHibernate. I am developing a C# application that has to interact with an existing database.Let say I have 2 tables: Items and ItemsList.

Items:     ID INT     ItemName VARCHAR(100)
ItemsList: ID INT     ChildItemID INT

I've built 2 classes and their mapping:

public class Items
{
    public virtual int id {get; set;}
    public virtual string itemName {get; set;}

}

public class ItemsMap : ClassMap<Items>
{
    public ItemsMap()
    {
        Id(x => x.id).GeneratedBy.Increment();
        Map(x => x.itemsName);
    }
}


public class ItemsList()
{
    public virtual int id {get; set;}
    public virtual IList<Items> childItems {get; set;}

    public ItemsList()
    {
        childItems = new List<Items>();
    }
}

public class ItemsListMap : ClassMap<ItemsList>
{        
    public ItemsListMap()
    {
        Id(x => x.id).GeneratedBy.Increment();
        HasMany(x => x.childItems).KeyColumn("childID").Cascade.All();
    }
}

And finally, I insert an item in the itemsList and save it all:

try
{
    using( ISession session = NH.OpenSession())
    {
        using(ITransaction transaction = session.BeginTransaction())
        {
            Items i = New Items()
            i = session.get<Items>(1);

            ItemsList il = new ItemsList();
            il.childID.Add(i);
            session.SaveOrUpdate(il);
            transaction.Commit();
        }
    }        
}

So when I commit, I have a new entry in ItemsList table, but the childID is blank.

Question:

All the examples I see has a reference to ItemsListID in Items table. But I don't want to have this reference since I want the item to be unique in the items table. How can I acheve that?

I am a beginner at using Fluent NHibernate. I am developing a C# application that has to interact with an existing database.Let say I have 2 tables: Items and ItemsList.

Items:     ID INT     ItemName VARCHAR(100)
ItemsList: ID INT     ChildItemID INT

我已经建立了2个类及其映射:

public class Items
{
    public virtual int id {get; set;}
    public virtual string itemName {get; set;}

}

public class ItemsMap : ClassMap<Items>
{
    public ItemsMap()
    {
        Id(x => x.id).GeneratedBy.Increment();
        Map(x => x.itemsName);
    }
}


public class ItemsList()
{
    public virtual int id {get; set;}
    public virtual IList<Items> childItems {get; set;}

    public ItemsList()
    {
        childItems = new List<Items>();
    }
}

public class ItemsListMap : ClassMap<ItemsList>
{        
    public ItemsListMap()
    {
        Id(x => x.id).GeneratedBy.Increment();
        HasMany(x => x.childItems).KeyColumn("childID").Cascade.All();
    }
}

最后,我把一个项目中的itemslist和挽救这一切:

try
{
    using( ISession session = NH.OpenSession())
    {
        using(ITransaction transaction = session.BeginTransaction())
        {
            Items i = New Items()
            i = session.get<Items>(1);

            ItemsList il = new ItemsList();
            il.childID.Add(i);
            session.SaveOrUpdate(il);
            transaction.Commit();
        }
    }        
}

所以当我犯了,我在itemslist表一个新条目,但childid是空白。

问题:

我所看到的所有实例参考itemslistid项目表。但我不想有这个引用,因为我希望项目在项目表中是唯一的。我怎么能达到呢?

answer1: 回答1:

The NHibernate native way for expressing the unique reference, is:

5.1.12. one-to-one

There are two varieties of one-to-one association:

  • primary key associations
  • unique foreign key associations

Primary key associations don't need an extra table column; if two rows are related by the association then the two table rows share the same primary key value. So if you want two objects to be related by a primary key association, you must make sure that they are assigned the same identifier value!...

Other words, Tables would look like this (Table Items generates the value of ItemID, table ItemsList takes that value and stores it in the ItemID ) :

Items:     ItemID INT     ItemName VARCHAR(100)
ItemsList: ItemID INT 

The C# would be (I changed Items into Item and ItemList into ItemMoreDetails, because it is not a list anymore)

public class Item
{
    public virtual int ItemId { get; set; }
    ...
    public virtual ItemMoreDetails ItemMoreDetails {get; set; }

public class ItemMoreDetails
{
    public virtual int ItemId { get; set; }
    ...
    public virtual Item Item {get; set;}

The mapping would be (in fluent):

// Parent side
public class ItemMap : ClassMap<Item>
{
    public ItemMap()
    {
        Id(x => x.id).GeneratedBy.Increment();
        ...
        HasOne(x => x.ItemMoreDetails).Cascade.All();

// child side
public class ItemMoreDetailsMap: ClassMap<ItemMoreDetails>
{
    public ItemMoreDetailsMap()
    {
        ...
        References(x => x.parent).Unique();

See the doc:

HasOne / one-to-one

表达独特的参考NHibernate原生的方式,是:

5.1.12。一对一的

有两种类型的一对一联系:

  • primary key associations
  • unique foreign key associations

主键关联不需要额外的表列;如果关联关联两行,则两个表行共享相同的主键值。因此,如果您希望一个主键关联关联两个对象,则必须确保它们被分配相同的标识符值!…

换句话说,表看起来像这样(表项产生对应,表itemslist以值并将其存储在对应的值):

Items:     ItemID INT     ItemName VARCHAR(100)
ItemsList: ItemID INT 

C #会(我改项目到项目和自行设计成itemmoredetails,因为它不是一个列表了)

public class Item
{
    public virtual int ItemId { get; set; }
    ...
    public virtual ItemMoreDetails ItemMoreDetails {get; set; }

public class ItemMoreDetails
{
    public virtual int ItemId { get; set; }
    ...
    public virtual Item Item {get; set;}

映射将是(流利):

// Parent side
public class ItemMap : ClassMap<Item>
{
    public ItemMap()
    {
        Id(x => x.id).GeneratedBy.Increment();
        ...
        HasOne(x => x.ItemMoreDetails).Cascade.All();

// child side
public class ItemMoreDetailsMap: ClassMap<ItemMoreDetails>
{
    public ItemMoreDetailsMap()
    {
        ...
        References(x => x.parent).Unique();

见文件:

HasOne / one-to-one

nhibernate  fluent-nhibernate  mapping  nhibernate-mapping  fluent-nhibernate-mapping