【问题标题】:Exception when deleting an object using fluent nhibernate table inheritance使用流利的 nhibernate 表继承删除对象时出现异常
【发布时间】:2012-06-13 13:09:17
【问题描述】:

使用流畅的 nhibernate 表继承删除对象时出现异常。

我无法弄清楚我的应用程序结构和映射有什么问题,以及为什么在我使用表继承时它正在寻找多对多映射表?

添加和更新工作正常。

我没有明确设置任何映射,我使用的是默认的 fluent nhibernate 映射。

对于处理我的级联的覆盖例外,如下所示:

public class CascadeAll : IHasOneConvention, IHasManyConvention, IReferenceConvention
{
    public void Apply(IOneToOneInstance instance)
    {
        instance.Cascade.All();
    }

    public void Apply(IOneToManyCollectionInstance instance)
    {
        instance.Cascade.All();
    }

    public void Apply(IManyToOneInstance instance)
    {
        instance.Cascade.All();
    }
} 

例外是:

Invalid object name 'BlogPageToPage'.

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 'BlogPageToPage'.

我的数据库是这样的。

Page
    Id (Guid)  
    Name  
    etc. 

BlogPage  
    Page_Id (Guid, exact same as parent page)  
   etc.

类:

public class Page : EntityBase 
{
    public Page()
    { 
        BlogPages = new List<BlogPage>(); 
    }

    public virtual IList<BlogPage> BlogPages { get; set; } 

}


public class BlogPage : Page
{
    public BlogPage()
    { 
    }

    public virtual IList<Post> Posts { get; set; } 
}

我的删除看起来像这样:

 public bool Delete(T model)
    {
        Session.Delete(model);
        return true;
    }

感谢您的意见。

【问题讨论】:

  • 我知道异常是因为缺少表,我的问题更多 - 当我的继承映射应该意味着不需要时,为什么要请求该表。谢谢。
  • 会不会和我数据库中的级联规则有关?

标签: c# nhibernate inheritance fluent-nhibernate


【解决方案1】:

“BlogPageToPage”与您的任何数据库或表或列名都不匹配...您能找到您提到“BlogPageToPage”对象的位置吗?

【讨论】:

  • 嗨,这是我问题的核心,根据我对表继承的理解,我的数据库中不应该需要那个表,它是异常的,因为它丢失了,但为什么它需要那个?
【解决方案2】:

我当然是个白痴。

它查找该表的原因是因为我将 BlogPages 作为父 Page 对象上的列表。

所以当我尝试删除它时,它只是认为那里应该有另一个表来匹配该关联。

违规行:

    public virtual IList<BlogPage> BlogPages { get; set; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多