Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无效强制转换(检查映射中的属性类型不匹配)_C#_Hibernate_Nhibernate - Fatal编程技术网

C# 无效强制转换(检查映射中的属性类型不匹配)

C# 无效强制转换(检查映射中的属性类型不匹配),c#,hibernate,nhibernate,C#,Hibernate,Nhibernate,场景 我在DB中有一个category类: CREATE TABLE [dbo].[Category]( [pk_cat_id] [int] NOT NULL, [name] [varchar](50) NOT NULL, [parent_cat_id] [int] NULL CONSTRAINT [PK_Category] PRIMARY KEY NONCLUSTERED ( [pk_cat_id] ASC )) Category类与自身有关联。它是一种

场景
我在DB中有一个category类:

CREATE TABLE [dbo].[Category](
    [pk_cat_id] [int] NOT NULL,
    [name] [varchar](50) NOT NULL,
    [parent_cat_id] [int] NULL
 CONSTRAINT [PK_Category] PRIMARY KEY NONCLUSTERED 
(
    [pk_cat_id] ASC
))

Category类与自身有关联。它是一种递归的双向关联(多对一和一对多)。 两者都引用相同的外键列:父项\类别\ id。
一个类别最多可以有一个父类别,并且没有或多个子类别。

这是Category.hbm.xml:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2"
                   assembly ="NHibernateIntro.Core"
                   namespace ="NHibernateIntro.Core.Domain">

  <class name="Category" table="Category">

    <id name="CategoryId" column="pk_cat_id">
      <generator class="hilo"/>
    </id>

    <property name="Name" column="name" type="string" length="50" not-null="true" />

    <many-to-one name="ParentCategory" class="Category" column="parent_cat_id" />

    <bag name="childCategories" cascade="all-delete-orphan" inverse="true">
      <key column="parent_cat_id"/>
      <one-to-many class="Category"/>      
    </bag>

  </class>
</hibernate-mapping>

这是Category.cs:

using System;
using System.Collections.Generic;
using Iesi.Collections.Generic;

namespace NHibernateIntro.Core.Domain
{
    public class Category
    {
        private Category parent_category;
        private ISet<Category> child_Categories = new HashedSet<Category>();

        public virtual int CategoryId { get; set; }
        public virtual string Name { get; set; }

        public Category() { }

        public Category( string cat_name )
        {
            Name = cat_name;
        }

        public virtual Category ParentCategory 
        {
            get
            {
                if (parent_category == null)
                    parent_category = new Category();

                return parent_category;
            }
            set{ parent_category = value; }
        }

        public virtual ISet<Category> childCategories
        {
            get { return child_Categories; }
            set { child_Categories = value; }
        } 
    }
}
使用系统;
使用System.Collections.Generic;
使用Iesi.Collections.Generic;
命名空间NHibernateIntro.Core.Domain
{
公共类类别
{
私有类父类;
私有ISet child_Categories=new HashedSet();
公共虚拟int类别ID{get;set;}
公共虚拟字符串名称{get;set;}
公共类别(){}
公共类别(字符串类别名称)
{
名称=类别名称;
}
公共虚拟类别父类别
{
得到
{
if(父类=空)
父类别=新类别();
返回父类;
}
设置{parent_category=value;}
}
公共虚拟ISet儿童类别
{
获取{return child_Categories;}
设置{child_Categories=value;}
} 
}
}
这是主要的方法:

public static void Run(ISessionFactory factory)
{
    int computerId = 1;

    using (ISession session = factory.OpenSession())
    using (session.BeginTransaction())
    {
        Category computer = session.Get<Category>(computerId); // **This line causes   Error(stated below)**
// Please see 'CONFUSING' tag below.
            Category laptops = new Category("Laptops");
            computer.childCategories.Add(laptops);
            laptops.ParentCategory = computer;
            session.Save(laptops);
            session.Transaction.Commit();
        }
    }
公共静态无效运行(ISessionFactory)
{
int computerId=1;
使用(ISession session=factory.OpenSession())
使用(session.BeginTransaction())
{
Category computer=session.Get(computerId);//**此行导致错误(如下所述)**
//请参阅下面的“混淆”标签。
类别笔记本电脑=新类别(“笔记本电脑”);
计算机。儿童分类。添加(笔记本电脑);
笔记本电脑.ParentCategory=电脑;
会话.保存(笔记本电脑);
Commit();
}
}
混乱:当我调试代码时,它停留在这一行:“set{parent_category=value;}”。我很困惑,因为我分配给Cateory,那个么为什么在这里调用parentCategory的setter呢

错误: 无效强制转换(检查映射中的属性类型不匹配); NHibernateIntro.Core.Domain.Category的setter

内部错误:无法强制转换“NHibernate.Collection.Generic.PersistentGenericBag
1[NHibernateIntro.Core.Domain.Category]类型的对象”
键入“Iesi.Collections.Generic.ISet
1[NHibernateIntro.Core.Domain.Category]”

请帮忙

在映射文件中使用set而不是bag。因为ISet不能强制转换到IList,而且包映射与.NET IList的更改兼容

private ISet<Category> child_Categories = new HashedSet<Category>();
private ISet child_Categories=new HashedSet();

private ICollection child_Categories=new HashSet();

它应该会起作用。注意,我使用的是C#HashSet,而不是Iesi.Collections HashSet。NHibernate的最新版本可能直接支持哈希集。

Yup。。那也很好。现在我又遇到了一个问题:session.Transaction.Commit();错误:对象引用了一个未保存的临时实例-在flushingYou可能想在另一个问题中询问该问题之前保存该临时实例。您的ParentCategory getter正在更新一个未显式保存的父类别,我认为它不会通过级联保存。我不会在getter中这样做,你应该在其他地方重新创建它。但我认为这可以通过cascade保存,但问题是:由于自引用,“ParentCategory”将尝试保存其父级,这将(永远)递归发生,最终导致StackOverFlow错误。此外,他还了解到,自我引用不是很好的做法。但是,如果插入的记录很少,这是很好的做法。
private ICollection<Category> child_Categories = new HashSet<Category>();