views:

427

answers:

2

Nothing happens when updating an entity using the SaveOrUpdate method with FluentNHibernate. Flush does work but I want to use SaveOrUpdate due to existing repository infrastructure. What could be the problem?

Configuration:

sessionFactory = Fluently.Configure()
    .Database(MsSqlConfiguration
        .MsSql2005
            .ConnectionString(c => c.FromConnectionStringWithKey("repository")))
        .Mappings(m => m.FluentMappings
            .AddFromAssemblyOf<InvoiceMap>())
    .BuildSessionFactory();

Mappings:

public InvoiceMap()
{
    Id(x => x.InvoiceID, "InvoiceID");
    Map(x => x.InvoiceNumber);
    Map(x => x.InvoiceDate);
    Map(x => x.Company).CustomTypeIs<CompanyType>();
    Map(x => x.TransactionNumber).CustomTypeIs<TransactionNumberType>();
    Map(x => x.LongAddressBookNumber);
    Map(x => x.PurchaseOrderNumber);
    Map(x => x.ReceivedDateTime);
    Map(x => x.OCR);
    Map(x => x.DocumentNumber);
    Map(x => x.DocumentType);
    Map(x => x.PaymentStatus).CustomTypeIs<PaymentStatusType>();
    HasMany(x => x.Attestations)
        .KeyColumnNames.Add("InvoiceID")
        .Inverse()
        .Cascade.AllDeleteOrphan()
        .AsBag();
    HasMany(x => x.AttestationRequests)
        .KeyColumnNames.Add("InvoiceID")
        .Inverse()
        .Cascade.AllDeleteOrphan()
        .AsBag();
    HasMany(x => x.States)
        .KeyColumnNames.Add("InvoiceID")
        .Inverse()
        .Cascade.AllDeleteOrphan()
        .AsBag();
}

public AttestationMap()
{
    Id(x => x.ID, "AttestationID");
    Map(x => x.ReceivedAt);
    Map(x => x.IsInvalid, "Invalid");
    Map(x => x.InvalidationReason);
    Map(x => x.FileName);
    Map(x => x.FileData);
    References<EmployeeSnapshot>(x => x.Certifier, "Certifier")
        .Cascade.All()
        .FetchType.Join();
    References<Invoice>(x => x.Owner, "InvoiceID");
}

public AttestationReminderMap()
{
    Id(x => x.ID, "AttestationReminderID");
    Map(x => x.CC)
        .CustomTypeIs<RecipientType>();
    Map(x => x.Message);
    Map(x => x.SentAt);
    References<AttestationRequest>(x => x.Owner, "RequestID");
}

Any ideas?

Thanks, Kristoffer

A: 

When you say nothing happens, do you mean that nothing happens to the DB without a flush?

If so, that is expected behavior. Flush tells NHib to write the changes it has in memory to the DB.

Chad Ruppert
Sorry, yes that it is what i mean. But then what is the purpose of the SaveOrUpdate method? I was reading the Best Practices guidlines, which suggesting that SaveOrUpdate should be used in layered application. Thanks
Kristoffer
SaveOrUpdate commmits the changes you made to the object to nhibs current session. The session is not written until you flush it, though. SaveOrUpdate vs Save or Update by itself automatically determines if it is a new instance of the object or not, and stores it appropriately.
Chad Ruppert
A: 

Somehow I had managed to set the session's FlushMode to Never. It resulted in the need for an explicit call to Flush, as Chad described to me. When FlushMode left to it default value, and using an Transaction, the entity is updated as expected.

Kristoffer