I have the following 2 classes:
Advertising
public virtual int Id { get; set; public virtual IList<AdvertImage> AdvertImages { get; set; }
Advertising
public virtual int Id { get; set; } public virtual string Filename { get; set; public virtual Advert Advert { get; set; }
In the DB, my ad table has FK 'AdvertId', which refers to the Adverts table, which has a PK identifier.
This is a one-to-many mapping because a single ad can have many images.
My current NHibernate mappings (edited for brevity):
Advertising
Id(x => x.Id) .GeneratedBy.Identity(); ... HasMany(x => x.AdvertImages) .KeyColumn("AdvertId") .Inverse(); ... Table("Adverts");
AdvertisingStore
Id(x => x.Id) .GeneratedBy.Identity(); ... References(x => x.Advert) .Column("AdvertId"); ... Table("AdvertImages");
I create a new instance of Advert in the code, then populating the AdvertImages property with the List<AdvertImage> .
When I go over to save my Advert object in the database, I would like AdvertImages to be inserted into their AdvertImages table, but due to the relationship between the two tables, I first need the insertion of the advertisement to occur, so how is the PK identifier generated, which then can be inserted into the table AdvertImages. (When I create my AdvertImage list, I populate the Filename property, but obviously there is no new advertisement at this point, so I want it to be filled when the advertisement is saved in the database).
I tried experimenting with different Inverse () and Cascade settings, but so far have failed. Can anyone help?
nhibernate fluent-nhibernate nhibernate-mapping fluent-nhibernate-mapping
marcusstarnes
source share