views:

452

answers:

3

I'm having a problem with Fluent NHibernate mappings, I think, and can't quite get past how I should be setting the mapping to avoid an issue.

I have a business object (literally, a "business"), and a review object. Each Business can have multiple reviews that are created on a page in the UI. The business is a property of the Review, as follows:

public class Business 
{
    public virtual int BusinessId {get;set;}
    public virtual DateTime LastModified {get;set;}
    public virtual IList<Review> Reviews {get;set;}

    [... more removed for brevity ...]

}

public class Review
{
    public virtual int ReviewId {get;set;}
    public virtual string ReviewText {get;set;}
    public virtual Business Business {get;set;}

    [... more removed for brevity ...]

}

My mappings are as follows:

public class ReviewMap : ClassMap<Review>
{
    public ReviewMap()
    {
        WithTable("Reviews");
        Id(x => x.ReviewId).TheColumnNameIs("ReviewId").GeneratedBy.Identity();

        References(x => x.Business).TheColumnNameIs("BusinessId");

        Map(x => x.ReviewText);

       [... and so on...]
}


public class BusinessMap : ClassMap<Business>
{
    public BusinessMap()
    {
        WithTable("Businesses");

        Id(x => x.BusinessId).TheColumnNameIs("BusinessId").GeneratedBy.Identity();

        Map(x => x.Name).TheColumnNameIs("BusinessName");
        Map(x => x.LastModified, "LastModifiedOn");

        HasMany<Review>(x => x.Reviews)
            .Inverse()
            .LazyLoad();

        [... more removed for brevity ...]

    }
}

The repository code is

public void Save(T entity)
    {
        using (ISession session = GetSession())
        using (ITransaction tx = session.BeginTransaction())
        {
            session.SaveOrUpdate(entity);
            tx.Commit();
        }
    }

In the code, I assign the properties to the Review object, and call the Repository's Save method.

The problem is that since I'm not updating the Business per se, I don't expect it to get saved--all I want is the review saved. But the code tries to save the Business as well, and I get an excption, as I haven't set the "LastModified" property--nor do I want to, as I'm saving the REVIEW, not the business.

How should I be setting up the mapping to let this happen?

A: 

The issue could be coming for a forign key requirement between the Business and Review. The business (parent) usually needs to be saved before the review (child) can be.

Could you include the mapping of your Business object if this doesn't help?

jasonlaflair
Well, the Business already exists--and I don't expect to be saving it, as it's sort of independent of the review--once I have the id, anyway. What I mean is this: why would I want to save an object that I'm not changing?
reallyJim
So, if the Business is saved, how are you getting an error saying the LastModified property isn't set? How are you assigning the Review.Business property value?
jasonlaflair
A: 

Ok, so the problem was this: the Business's LastModified property wasn't actually set in the actual business object, as the database allowed NULL for this property.

What that meant was that when I tried to save the review, it also tried to save the business--most likely because it recognized that the database object had no value (null) for LastModified, but by default, since LastModified is a DateTime, its value was DateTime.MinValue.

Once I updated the business table in the database so that LastModified is now NOT NULL, it works.

The unfortunate part of this is that the data I was working with got into this state without this being an issue.

reallyJim
if this answers your question, you should accept this as the answer.
ddc0660
Sorry that took so long--I had to wait to accept my own answer, and honestly...forgot to come back and fix it!
reallyJim
A: 

As I can't comment yet, I'll post here.

If you did want the LastModified column to be nullable, the corresponding property should have been a DateTime? rather than a plain DateTime.

Dave W
Dave is correct here, if your whole issue is just with the DateTime needing to be null... You should be using DateTime? instead of setting your DateTime to DateTime.Min.
jasonlaflair