I am working with a legacy database and I am trying to map some kind of localization table with Fluent NHibernate.
The table looks like this:
DataName
[PK] TableName
[PK] FieldName
[PK] Id
[PK] LangId
Description
This table is mapped through this entity:
public class DataName : EntityBase { public virtual string TableName { get; set; } public virtual string FieldName { get; set; } public virtual int Id { get; set; } public virtual Language Language { get; set; } public virtual string Description { get; set; } ... } public class DataNameMap : ClassMap { public DataNameMap() { Table("zDataName"); CompositeId().KeyProperty(x => x.TableName) .KeyProperty(x => x.FieldName) .KeyProperty(x => x.Id) .KeyProperty(x => x.Language, "LangId").CustomType(); Map(x => x.Description).Column("Descr").Length(2000); } }
I have multiple entities mapped to the "DataName" entity for the purpose of localization and the mapping looks like this:
public class Carrier { public virtual int CarrId { get; set; } public virtual string CarrCode { get; set; } public virtual List DataNameList { get; set; } ... } public class CarrierMap : ClassMap { public CarrierMap() { Table("Carriers"); Id(x => x.CarrId).GeneratedBy.Identity(); Map(x => x.CarrCode).Not.Nullable().Length(8); HasMany(x => x.DataNameList) .KeyColumn("Id") .Where("TableName = 'carriers' AND FieldName = 'name'") .Inverse() .Cascade.AllDeleteOrphan(); ... } }
The "DataName" table contains records that looks like the following:
TableName FieldName Id LangId Description --------- --------- -- ------ ----------- Carriers Name 1000 1 English description Carriers Name 1000 2 French description
I can retrieve this information from the database without any problem, but when comes the time to save a new entity (insert or update) to the database, NHibernate dosen't perform the operations in the right order.
INSERT INTO DataName (TableName, FieldName, LangId, Id, Description) VALUES ('Carriers', 'Name', 1, NULL, 'English description') INSERT INTO DataName (TableName, FieldName, LangId, Id, Description) VALUES ('Carriers', 'Name', 2, NULL, 'French description') INSERT INTO Carriers....
For the purpose of this example, let's say the id generated by the database is 2000.
UPDATE DataName SET Id = 2000 WHERE TableName = 'Carriers' AND FieldName = 'Name' AND LangId = 1 AND Id IS NULL UPDATE DataName SET Id = 2000 WHERE TableName = 'Carriers' AND FieldName = 'Name' AND LangId = 2 AND Id IS NULL
I have tried playing with the Inverse() option of my HasMany mapping for the DataNameList collection, but it doesn't seem to have any effect on this particular situation.
How do I tell NHibernate to persist the "Carrier" entity and then the collection of "DataName" without having to issue additional UPDATE statement after the "Carrier" entity is inserted?