Nhibernate 如何删除记录?

Nhibernate 如何删除记录?,nhibernate,asp.net-mvc-2,fluent-nhibernate,Nhibernate,Asp.net Mvc 2,Fluent Nhibernate,我正在尝试使用ASP.NET MVC、Fluent和NHibernate删除数据库记录。请参阅下面的代码,了解我如何尝试实现这一点的示例。我能够获取、更新和插入记录,但删除不起作用。在控制器(顶部)中调用Delete()方法时,它会引发异常(System.Data.SqlClient.SqlException:无效的对象名“Styles”。) 我希望避免任何类型的meta-SQL查询,因为如果不需要,我不想将表名硬编码到控制器中 控制器代码段: // POST: /Brand/Delete/5

我正在尝试使用ASP.NET MVC、Fluent和NHibernate删除数据库记录。请参阅下面的代码,了解我如何尝试实现这一点的示例。我能够获取、更新和插入记录,但删除不起作用。在控制器(顶部)中调用Delete()方法时,它会引发异常(
System.Data.SqlClient.SqlException:无效的对象名“Styles”。

我希望避免任何类型的meta-SQL查询,因为如果不需要,我不想将表名硬编码到控制器中

控制器代码段

// 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;
    }
}
//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();
        }
    }
}
//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();
    }
}
//POST:/Brand/Delete/5
//这是控制器中的处理程序
[HttpPost]
公共操作结果删除(int-id,FormCollection集合)
{
尝试
{
IRepository repo=新品牌存储库();
回购协议删除(id);
返回操作(“索引”);
}
抓住
{
投掷;
}
}
存储库代码段

// 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;
    }
}
//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();
        }
    }
}
//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();
    }
}
//这是存储库
//插入/更新/获取/等都可以正常工作
作废IRepository.Delete(int-id)
{
使用(ISession session=NHibernateHelper.OpenSession())
{
使用(ITransaction transaction=session.BeginTransaction())
{
IRepository repo=新品牌存储库();
删除(repo.GetById(id));
Commit();
}
}
}
映射代码段

// 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;
    }
}
//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();
        }
    }
}
//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();
    }
}
//下面是一个品牌的映射
公共类BrandMap:ClassMap
{
公共品牌地图()
{
表(“品牌”);
Id(x=>x.Id).GeneratedBy.Identity();
Map(x=>x.Name);
有许多(x=>x.Styles)
.Inverse()
.Cascade.All();
}
}

看起来
样式
属性的映射不正确。您是否使用了正确的表名?从异常来看,似乎没有这样的表
style

引发了什么异常?就硬编码而言,您已经在控制器中硬编码了一个存储库,因此您的代码与NHibernate紧密耦合。捕获异常而不实际处理它,例如记录它是非常糟糕的做法。还有,你到底为什么要在
Delete
方法中创建一个
BrandRepository
的新实例,这个方法看起来已经是这个类的一部分了?很难确定抛出了哪个异常,因为我必须返回一个视图,否则它就无法很好地构建并放置一个
捕获(exception ex)
并调试
ex
的值,或者更好,删除
try/catch
块并保留异常传播。这3个代码段是独立的文件。Stackoverflow上的格式设置不允许我很容易地将它们分开。我会试着编辑一下,好的。请看我的编辑