流畅的NHibernate HASMANY映射没有引用

时间:2020-12-08 11:30:26

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.

我是使用Fluent NHibernate的初学者。我正在开发一个必须与现有数据库交互的C#应用​​程序。假设我有2个表:Items和ItemsList。

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

I've built 2 classes and their mapping:

我已经构建了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();
    }
}

And finally, I insert an item in the itemsList and save it 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();
        }
    }        
}

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

所以当我提交时,我在ItemsList表中有一个新条目,但是childID是空的。

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?

我看到的所有示例都引用了Items表中的ItemsListID。但是我不希望有这个引用,因为我希望item在items表中是唯一的。我该怎么做呢?

1 个解决方案

#1


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

用于表达唯一引用的NHibernate本地方式是:

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 ) :

换句话说,表格看起来像这样(Table Items生成ItemID的值,表ItemsList获取该值并将其存储在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)

C#将是(我将Item更改为Item,将ItemList更改为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;}

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

#1


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

用于表达唯一引用的NHibernate本地方式是:

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 ) :

换句话说,表格看起来像这样(Table Items生成ItemID的值,表ItemsList获取该值并将其存储在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)

C#将是(我将Item更改为Item,将ItemList更改为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;}

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