简体   繁体   中英

FluentNhibernate, specify or autogenerate Foreign Key field?

I'm learning FluentNHibernate, and until now I haven't specified FKs as property fields, but I let them be auto-generated, from the mappings.

But is that sufficient? It looks to me that in this case, if I want to save some object, I can only save (new objects) by saving the whole aggregate root. While I sometimes see it more appropriate to save some lower level objects only.

So this question I think goes into the use of aggregate roots. How can they be structured? I thought one aggregate could be part of another aggregate, which would mean you could actually save lower level aggregate without saving the whole root. Is this true? And in this case, don't we need to specify the FK if we deal with a newly added entity? Hence this field must be coded in our classes, and not simply auto-generated as I'm currently doing?

If you really want to save a single entity, and not the aggregate root, you will need to define the property that reference the parent entity, allowing you to set it and save the single entity. Personally I don't see any issues with it. Though apparently this is not good DDD practice .

To save a lower level entity and not it's aggregate root you will need to specify the parent anyways. I guess that is why is better to work with the root directly.

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