城堡活动记录 - 一对多的关系

时间:2011-11-02 13:03:58

标签: nhibernate castle-activerecord relationship

我尝试在Active Record中创建多对一对多的关系。

[ActiveRecord("Users")]
public class User : ActiveRecordBase<User>
{
    private IList<PhotoAlbum> _albums = new List<PhotoAlbum>();

    [PrimaryKey]
    public virtual int UserId { get; set; }

    [BelongsTo("ProfilId")]
    public virtual Profil Profil { get; set; }

    [HasMany(Inverse = false,Cascade = ManyRelationCascadeEnum.Delete)]
    public IList<PhotoAlbum> Album
    {
        get { return _albums; }
        set { _albums = value; }
    }

    [Property]
    public virtual string Nick { get; set; }

}

[ActiveRecord("Profiles")]
public class Profil : ActiveRecordBase<Profil>
{
    [PrimaryKey]
    public int ProfilId { get; set; }

    [Property]
    public int Age { get; set; }
    [Property]
    public int Sex { get; set; }
    [Property]
    public string Region { get; set; }
    [Property]
    public string Town { get; set; }
    [Property]
    public bool WithPhoto { get; set; }
    [Property]
    public bool HasPhotoAlbum { get; set; }
}

[ActiveRecord("PhotoAlbums")]
public class PhotoAlbum : ActiveRecordBase<PhotoAlbum>
{
    [PrimaryKey]
    public int PhotoAlbumId { get; set; }

    [Property]
    public string Name { get; set; }
    [Property]
    public int NumberOfPhoto { get; set; }
}

我正在使用http://www.castleproject.org/activerecord/gettingstarted/relations.html

中的示例

它完成了这个错误:

  

ActiveRecord尝试推断有关User.Album关系的详细信息   它无法在目标类型中找到'BelongsTo'映射属性

StrackTrace:

  

在   Castle.ActiveRecord.Framework.Internal.SemanticVerifierVisitor.VisitHasMany(HasManyModel   模特)   Castle.ActiveRecord.Framework.Internal.HasManyModel.Accept(IVisitor   访客)   Castle.ActiveRecord.Framework.Internal.AbstractDepthFirstVisitor.VisitNode(IVisitable   可访问的)   Castle.ActiveRecord.Framework.Internal.AbstractDepthFirstVisitor.VisitNodes(IEnumerable的   节点)   Castle.ActiveRecord.Framework.Internal.AbstractDepthFirstVisitor.VisitModel(ActiveRecordModel   模特)   Castle.ActiveRecord.Framework.Internal.SemanticVerifierVisitor.VisitModel(ActiveRecordModel   模特)   Castle.ActiveRecord.Framework.Internal.ActiveRecordModel.Accept(IVisitor   访客)   Castle.ActiveRecord.Framework.Internal.AbstractDepthFirstVisitor.VisitNode(IVisitable   可访问的)   Castle.ActiveRecord.Framework.Internal.AbstractDepthFirstVisitor.VisitNodes(IEnumerable的   节点)   Castle.ActiveRecord.ActiveRecordStarter.RegisterTypes(ISessionFactoryHolder   holder,IConfigurationSource source,IEnumerable`1 types,Boolean   ignoreProblematicTypes)at   Castle.ActiveRecord.ActiveRecordStarter.Initialize(IConfigurationSource   源,类型[]类型)在SAMPLE_1.Program.Main(String [] args)中   E:\ C#PROJECTS \ STUDY \ STUDY.ORM \ Active Record

     
    

SAMPLE_1.PhotoAlbum

  

我认为问题在于:

    [HasMany(Inverse = false,Cascade = ManyRelationCascadeEnum.Delete)]
    public IList<PhotoAlbum> Album
    {
        get { return _albums; }
        set { _albums = value; }
    }

如果我省略了这个属性/关系,它就可以了。

1 个答案:

答案 0 :(得分:0)

假设一个个人资料有很多相册,那么您的PhotoAlbum课程缺少一个属性:

[BelongsTo("ProfileId")]
public Profile Profile { get; set; }