流利的NH - 非法访问装载集合

时间:2010-07-20 00:13:22

标签: nhibernate fluent-nhibernate nhibernate-mapping

在CategoriesTranslated集合中,我有这样的错误:非法访问加载集合。

 public class Category : Entity
{
    public Category()
    {
        CategoriesTranslated = new List<CategoryTranslated>();

    }

    public virtual Category Parent { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<CategoryTranslated> CategoriesTranslated { get; set; }
}

public class CategoryTranslated : Entity
{
    public CategoryTranslated()
    {
    }

    public virtual Category Category { get; set; }
    public virtual LanguageType Language { get; set; }
    public virtual string Name { get; set; }
}


   public void Override(AutoMapping<Category> mapping)
    {
        mapping.HasMany(x => x.CategoriesTranslated)
            .Inverse()
            .Cascade.All();

    }

  public void Override(AutoMapping<CategoryTranslated> mapping)
    {
        mapping.References(x => x.Category);
    }

SQL:

CREATE TABLE Category(
   [Id] smallint primary key identity(1,1),
   [Parent] smallint null,
   [Name] varchar(50) not null unique,
   )
    alter table [Category] add CONSTRAINT fk_Category_Category  
FOREIGN KEY(Parent) references Category (Id)
go


    CREATE TABLE CategoryTranslated(
   [Id] smallint primary key identity(1,1),
   [Category] smallint not null,
   [Language] tinyint not null,
   [Name] varchar(50) not null,
   )

    alter table [CategoryTranslated] add CONSTRAINT fk_CategoryTranslated_Category  
 FOREIGN KEY(Category) references Category (Id)
 go

哪里错了?

UPDATE 与hbm生成器的链接:

类别:
http://uploading.com/files/fmb71565/SubmitSiteDirectory.Core.Category.hbm.xml/

分类翻译: http://uploading.com/files/9c9aaem9/SubmitSiteDirectory.Core.CategoryTranslated.hbm.xml/

1 个答案:

答案 0 :(得分:1)

我猜它与构造函数中的列表创建有关,特别是如果你为NHib留下了一个默认的ctor。 NHib正在尝试在创建之前设置列表。另一个复杂因素是你有一个双向关系,而且CategoryTranslated也可能在它创建之前试图进入列表。

我怀疑这是唯一的解决方案,但是我使用的模式应该可以解决错误:

    /// <summary>Gets the ....</summary>
    /// <remarks>This is what is available to outside clients of the domain.</remarks>
    public virtual IEnumerable<CategoryTranslated> CategoriesTranslated{ get { return _InternalCategoriesTranslated; } }

    /// <summary>Workhorse property that maintains the set of translated categories by:
    /// <item>being available to <see cref="Category"/> to maintain data integrity.</item>
    /// <item>lazily instantiating the <see cref="List{T}"/> when it is needed.</item>
    /// <item>being the property mapped to NHibernate, the private <see cref="_categoriesTranslated"/> field is set here.</item>
    /// </list>
    /// </summary>
    protected internal virtual IList<Category> _InternalCategoriesTranslated
    {
        get { return _categoriesTranslated?? (_categoriesTranslated= new List<Category>()); }
        set { _categoriesTranslated= value; }
    }
    private IList<StaffMember> _categoriesTranslated;

现在您需要设置映射以访问私有字段,因此假设您在此处使用我的外壳首选项,您将拥有:

public void Override(AutoMapping<Category> mapping)
{
    mapping.HasMany(x => x.CategoriesTranslated)
        .Inverse()
        .Access.CamelCaseField(CamelCasePrefix.Underscore)
        .Cascade.All();
}

HTH,
Berryl

编辑============

_Internal集合还为子节点提供了双向关系,在这种情况下为CategoryTranslated,一个钩子,如下面的代码所示:

    public virtual CategoryTranslated CategoryTranslated
    {
        get { return _categoryTranslated; }
        set
        {
            if (_categoryTranslated!= null)
            {
                // disassociate existing relationship
                _categoryTranslated._InternalCategoryTranslated.Remove(this);
            }

            _categoryTranslated= value;

            if (value != null)
            {
                //make the association
                _categoryTranslated._InternalCategoryTranslated.Add(this);
            }
        }
    }
    private CategoryTranslated _categoryTranslated;