chobo2
chobo2

Reputation: 85765

How to update an object with nhibernate + fluent nhibernate?

I do a query through nhibernate to get a record back from the database.

var result  = session.Query<TableA>().Where(x => x.Id == 10).FirstOrDefault();
result.Where = "hi";
session.Update(result)
session.Commit();

So I get a result back and update a property and try to update and then commit it. This will crash because I have forigen key to Table B(Table A can Has one Table B and Table B has many Table A's)

So this reference cannot be null. So when it tries to do an update it crashes.

So I have to do

       var result  = session.Query<TableA>().Where(x => x.Id == 10).FirstOrDefault();
        result.Where = "hi";
        result.TableB = session.Load<TableB>(1);
        session.Update(result)
    session.Commit();

Then it is happy.

How can I do an update without having to load TableB in?

Upvotes: 1

Views: 2110

Answers (1)

csano
csano

Reputation: 13686

Set the Update value of the TableB property mapping to false. I'm not sure how you'd do this using Fluent NHibernate, but it looks like the SetAttribute method will do this for you.

Something along the lines of: .SetAttribute("update", "false");

Upvotes: 1

Related Questions