views:

26

answers:

1

I'm attempting to do the most simple of mappings with FluentNHibernate & Sql2005. Basically, I have a database table called "sv_Categories". I'd like to add a category, setting the ID automatically, and adding the userid and title supplied.

Database table layout:

  • CategoryID -- int -- not-null, primary key, auto-incrementing
  • UserID -- uniqueidentifier -- not null
  • Title -- varchar(50) -- not null

Simple.

My SessionFactory code (which works, as far as I can tell):
            _SessionFactory = Fluently.Configure().Database(
                MsSqlConfiguration.MsSql2005
                    .ConnectionString(c => c.FromConnectionStringWithKey("SVTest")))
                    .Mappings(x => x.FluentMappings.AddFromAssemblyOf<CategoryMap>())
                    .BuildSessionFactory();

My ClassMap code:

public class CategoryMap : ClassMap<Category>
{
    public CategoryMap()
    { 
        Id(x => x.ID).Column("CategoryID").Unique();
        Map(x => x.Title).Column("Title").Not.Nullable();
        Map(x => x.UserID).Column("UserID").Not.Nullable();
    }
}

My Class code:

public class Category
{
    public virtual int ID { get; private set; }
    public virtual string Title { get; set; }
    public virtual Guid UserID { get; set; }

    public Category()
    { 
        // do nothing 
    }
}

And the page where I save the object:

    public void Add(Category catToAdd)
    {
        using (ISession session = SessionProvider.GetSession())
        {
            using (ITransaction Transaction = session.BeginTransaction())
            {
                session.Save(catToAdd);
                Transaction.Commit();
            }
        }
    }

I receive the error

Invalid object name 'Category'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'Category'.

I think it might be that I haven't told the CategoryMap class to use the "sv_Categories" table, but I'm not sure how to do that.

Any help would be appreciated. Thanks!

+1  A: 

Use the Table method in your ClassMap.

public class CategoryMap : ClassMap<Category>
{
  public CategoryMap()
  {
    Table("sv_Categories"); 
  }
}
James Gregory
That did it! Thank you so much! :)
goober