简体   繁体   English

流利的NHibernate CompositeId尝试插入空值

[英]Fluent NHibernate CompositeId trying to insert null values

I am working to map an existing database using Fluent NHibernate and have encountered a problem when it comes to complex many-to-many relationships (additional columns). 我正在使用Fluent NHibernate映射现有数据库,并且在涉及复杂的多对多关系(附加列)时遇到了问题。

I know that many-to-many relationships with additional columns have to be mapped as HasMany rather than HasManyToMany as they are not pure many-to-many relationships. 我知道具有附加列的多对多关系必须映射为HasMany而不是HasManyToMany因为它们不是纯粹的多对多关系。 The linking table has to be mapped as a class within itself, which I have done in the example below. 链接表必须在自身内部映射为类,这在下面的示例中已经完成。

When loading this data from an existing database it loads fine. 从现有数据库加载此数据时,它会很好地加载。 The project I am working on takes this data and inserts it into an empty database, which is where the problem occurs. 我正在处理的项目将这些数据插入到空数据库中,这就是问题所在。 I think that when inserting into the new database the CompositeId is trying to insert NULL values for ItemID and ItemGroupID which is not allowed in the database. 我认为,当插入新数据库时, CompositeId试图为数据库中不允许的ItemIDItemGroupID插入NULL值。 Changing the database structure is not a viable option at this point, is there a way around this issue? 目前,更改数据库结构不是可行的选择,是否可以解决此问题?

Thanks, example code below. 谢谢,下面的示例代码。


Entity Classes 实体类别

public class Item
{
    public virtual long ItemID { get; set; }
    public virtual string Name { get; set; }
}

public class ItemGroup
{
    public virtual long ItemGroupID { get; set; }
    public virtual string Name { get; set; }

    public virtual IList<ItemInGroup> ItemsInGroup { get; set; }
}

public class ItemInGroup
{
    public virtual Item Item { get; set; }
    public virtual ItemGroup ItemGroup { get; set; }
    public virtual int? DisplayOrder { get; set; }
}

Mapping Classes 映射类

public class ItemMap : ClassMap<Item>
{
    public ItemMap()
    {
        Table("Items");
        Id(x => x.ItemID).GeneratedBy.Identity();

        Map(x => x.Name);
    }
}

public class ItemGroupMap : ClassMap<ItemGroup>
{
    public ItemGroupMap()
    {
        Table("ItemGroups");
        Id(x => x.ItemGroupID).GeneratedBy.Identity();

        Map(x => x.Name);
        HasMany(x => x.ItemsInGroup).KeyColumn("ItemGroupID").Cascade.All();
    }
}

public class ItemInGroupMap : ClassMap<ItemInGroup>
{
    public ItemInGroupMap()
    {
        Table("ItemsInGroups");

        CompositeId().KeyReference(x => x.Item, "ItemID")
                     .KeyReference(x => x.ItemGroup, "ItemGroupID");

        Map(x => x.DisplayOrder);
    }
}

assuming DisplayOrder is the only extra column in the link table why not use the List index of IList as order? 假设DisplayOrder是链接表中唯一的额外列,为什么不使用IList的List索引作为顺序?

public class ItemGroup
{
    public virtual long ItemGroupID { get; set; }
    public virtual string Name { get; set; }

    public virtual IList<Item> Items { get; private set; }
}

public class ItemGroupMap : ClassMap<ItemGroup>
{
    public ItemGroupMap()
    {
        Table("ItemGroups");
        Id(x => x.ItemGroupID).GeneratedBy.Identity();

        Map(x => x.Name);
        HasManyToMany(x => x.ItemsInGroup)
            .Table("ItemsInGroups")
            .ParentKeyColumn("ItemGroupID")
            .ChildKeyColumn("ItemID")
            .AsList("DisplayOrder")
            .Cascade.All();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM