R – FluentNHibernate: Invalid object name on SaveOrUpdate

fluent-nhibernate

I am trying to get FluentNHibernate up and running.

When I try to add a entity object to the db I get "invalid object name 'Recipe' error in the inner exception and this the main exception

could not insert: [OurRecipes.Domain.Recipe][SQL: INSERT INTO Recipe (EnteredByID, ModifiedOn, Method, PrepTime, CookTime, RecipeTitle) VALUES (?, ?, ?, ?, ?, ?); select SCOPE_IDENTITY()]

This is how i am configuring it.

public static ISessionFactory Create()
    {
        return Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2005
            .ConnectionString(c => c.Is("Server=(local);initial catalog=OurRecipesDB;Integrated Security=SSPI")))
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Recipe>())
            .BuildSessionFactory();
    }

This is my first go at this so….

It doesn't seem to recognise 'Recipe' class in my domain project even though
this configuration runs with no errors .

This the code where I get the error on the SaveOrUpdate line.

        var factory = SessionFactoryCreator.Create();

        using (var session = factory.OpenSession())
        {

            Recipe rec = new Recipe();
            rec.RecipeTitle = "test";
            rec.Method = "test";
            rec.PrepTime = 10;
            rec.CookTime = 20;
            rec.EnteredByID = 1;
            rec.ModifiedOn = DateTime.Now;

            session.SaveOrUpdate(rec);

}

EDIT: Sorry guys the SQL table name is Recipes…..Well its late here!

Best Solution

What's your mapping for Recipe look like? My bet is that you didn't define the table name. Your mapping class should look something like:

public class RecipeMap : ClassMap<Recipe>
{
    public RecipeMap()
    {
        SchemaIs("dbo");
        WithTable("Recipes");
        [... additional mappings ...]
    }
}
Related Question