简体   繁体   中英

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

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. 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. 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?

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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