简体   繁体   中英

fluent nhibernate automapping one to many makes foreign key null

I am relatively new to NHberinate, and am using it as an alternative to EF cause it works nicely on Mono.

All my entities are mapped with AutoMapping.

The problem is I have a one to many relationship setup. One of Item class has an of localizedText class. I also setup a convention for Cascade all.

 public class CascadeAll : IHasOneConvention, IHasManyConvention, IReferenceConvention
 {
    public void Apply(IOneToOneInstance instance)
    {
        instance.Cascade.All();
    }

    public void Apply(IOneToManyCollectionInstance instance)
    {
        instance.Cascade.All();

    }

    public void Apply(IManyToOneInstance instance)
    {
        instance.Cascade.All();
    }
}

When I add a localizedText to my Item class, that works well, and the localizedText table gets values written into the table, however..the foreign key value "itemId" remains null. How can I make it work so that the localizedText items have an objectId when added?

Zack

Great when you find the answer to your own questions.

In order for the correct SQL to be executed especially with any objects that have relationships, the code for saving the objects should be between the following method calls. begintransaction() and commitTransaction(). Only when commitTransaction is called, are all values written to the database, even fields that have no value are automatically assigned a zero, instead of null in the earlier case.

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