I'm trying to delete a database record using ASP.NET MVC, Fluent, and NHibernate. See the code below for examples of how I'm trying to accomplish this. I am able to Get, Update, and Insert records but Delete is not working. When the Delete() method gets called in the controller (top one) it throws an Exception (System.Data.SqlClient.SqlException: Invalid object name 'Styles'.
).
I would like to avoid any sort of meta-SQL query because I don't want to hard code the table name into the controller if I don't have to.
Controller snippet:
// POST: /Brand/Delete/5
// Here is the handler in the controller
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
IRepository<Brand> repo = new BrandRepository();
repo.Delete(id);
return RedirectToAction("Index");
}
catch
{
throw;
}
}
Repository snippet:
//Here is the repository
//The insert/update/get/etc all work fine
void IRepository<Brand>.Delete(int id)
{
using (ISession session = NHibernateHelper.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
IRepository<Brand> repo = new BrandRepository();
session.Delete(repo.GetById(id));
transaction.Commit();
}
}
}
Mapping snippet:
//And here is the mapping for a Brand
public class BrandMap : ClassMap<Brand>
{
public BrandMap()
{
Table("Brands");
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.Name);
HasMany(x => x.Styles)
.Inverse()
.Cascade.All();
}
}