Castle Active Record - 一对多关系

发布于 2024-12-13 04:54:14 字数 3214 浏览 2 评论 0原文

我尝试在活动记录中创建多对多关系。

[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 中找到“BelongsTo”映射属性:

在 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 源,IEnumerable`1 类型,布尔值 忽略ProblematicTypes)在 Castle.ActiveRecord.ActiveRecordStarter.Initialize(IConfigurationSource 源,Type[] 类型)位于 SAMPLE_1.Program.Main(String[] args) 中 E:\C# PROJECTS\STUDY\STUDY.ORM\Active Record

<块引用>

SAMPLE_1.相册

我认为问题出在这里:

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

如果我省略了这个属性/关系,它会很好地工作。

I try create many to relationship one to many in 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; }
}

I am using sample from http://www.castleproject.org/activerecord/gettingstarted/relations.html

it finished with this error:

ActiveRecord tried to infer details about the relation User.Album but
it could not find a 'BelongsTo' mapped property in the target type

StrackTrace:

at
Castle.ActiveRecord.Framework.Internal.SemanticVerifierVisitor.VisitHasMany(HasManyModel
model) at
Castle.ActiveRecord.Framework.Internal.HasManyModel.Accept(IVisitor
visitor) at
Castle.ActiveRecord.Framework.Internal.AbstractDepthFirstVisitor.VisitNode(IVisitable
visitable) at
Castle.ActiveRecord.Framework.Internal.AbstractDepthFirstVisitor.VisitNodes(IEnumerable
nodes) at
Castle.ActiveRecord.Framework.Internal.AbstractDepthFirstVisitor.VisitModel(ActiveRecordModel
model) at
Castle.ActiveRecord.Framework.Internal.SemanticVerifierVisitor.VisitModel(ActiveRecordModel
model) at
Castle.ActiveRecord.Framework.Internal.ActiveRecordModel.Accept(IVisitor
visitor) at
Castle.ActiveRecord.Framework.Internal.AbstractDepthFirstVisitor.VisitNode(IVisitable
visitable) at
Castle.ActiveRecord.Framework.Internal.AbstractDepthFirstVisitor.VisitNodes(IEnumerable
nodes) at
Castle.ActiveRecord.ActiveRecordStarter.RegisterTypes(ISessionFactoryHolder
holder, IConfigurationSource source, IEnumerable`1 types, Boolean
ignoreProblematicTypes) at
Castle.ActiveRecord.ActiveRecordStarter.Initialize(IConfigurationSource
source, Type[] types) at SAMPLE_1.Program.Main(String[] args) in
E:\C# PROJECTS\STUDY\STUDY.ORM\Active Record

SAMPLE_1.PhotoAlbum

I think problem is here:

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

If I omitted this property / relationship it works good.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑红尘 2024-12-20 04:54:14

假设一个配置文件有许多相册,您的 PhotoAlbum 类缺少一个属性:

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

Assuming one profile has many photo albums, your PhotoAlbum class is missing a property:

[BelongsTo("ProfileId")]
public Profile Profile { get; set; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文