简体   繁体   English

流利的 nhibernate 自动映射一对多使外键 null

[英]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.我对 NHberinate 比较陌生,并且正在使用它作为 EF 的替代品,因为它在 Mono 上运行良好。

All my entities are mapped with AutoMapping.我所有的实体都使用 AutoMapping 进行映射。

The problem is I have a one to many relationship setup.问题是我有一个一对多的关系设置。 One of Item class has an of localizedText class.项目 class 之一具有本地化文本 class。 I also setup a convention for Cascade all.我还为 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.当我将localizedText 添加到我的项目class 时,效果很好,并且localizedText 表将值写入表中,但是......外键值“itemId”仍然是null。 How can I make it work so that the localizedText items have an objectId when added?我怎样才能使它工作,以便localizedText项目在添加时有一个objectId?

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.为了执行正确的 SQL,尤其是对于任何具有关系的对象,保存对象的代码应位于以下方法调用之间。 begintransaction() and commitTransaction(). begintransaction() 和 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.只有在调用 commitTransaction 时,才会将所有值写入数据库,即使是没有值的字段也会自动分配零,而不是前面的 null。

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

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