SeanKilleen
SeanKilleen

Reputation: 8977

FluentNHibernate error -- "Invalid object name"

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:

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!

Upvotes: 1

Views: 2986

Answers (1)

James Gregory
James Gregory

Reputation: 14223

Use the Table method in your ClassMap.

public class CategoryMap : ClassMap<Category>
{
  public CategoryMap()
  {
    Table("sv_Categories"); 
  }
}

Upvotes: 5

Related Questions