Castle ActiveRecord - 在不强制引用完整性的情况下生成模式?

发布于 2024-08-30 16:54:24 字数 1359 浏览 3 评论 0原文

我刚刚开始使用 Castle active record,因为它似乎是进入 NHibernate 的一种温和方式。我真的很喜欢在开发过程中从我的类生成数据库模式的想法。

我想做类似以下的事情:

[ActiveRecord]
public class Camera : ActiveRecordBase<Camera>
{
    [PrimaryKey]
    public int CameraId {get; set;}
    [Property]
    public int CamKitId {get; set;}
    [Property]
    public string serialNo {get; set;}
}

[ActiveRecord]
public class Tripod : ActiveRecordBase<Tripod>
{
    [PrimaryKey]
    public int TripodId {get; set;}
    [Property]
    public int CamKitId {get; set;}
    [Property]
    public string serialNo {get; set;}
}

[ActiveRecord]
public class CameraKit : ActiveRecordBase<CameraKit>
{
    [PrimaryKey]
    public int CamKitId {get; set;}
    [Property]
    public string description {get; set;}
    [HasMany(Inverse=true, Table="Cameras", ColumnKey="CamKitId")]
    public IList<Camera> Cameras {get; set;}
    [HasMany(Inverse=true, Table="Tripods", ColumnKey="CamKitId")]
    public IList<Camera> Tripods {get; set;}

}

相机套件应包含任意数量的三脚架和相机。相机套件独立于相机和三脚架而存在,但有时是相关的。

问题是,如果我使用createschema,这将对相机和三脚架表施加外键约束。我不想要这个,我希望能够在三脚架和相机表上将 CamKitId 设置为 null,以表明它不是 CameraKit 的一部分。

有没有办法告诉 activerecord/nhibernate 仍然将其视为相关的,而不强制执行完整性?我想我可以在那里有一个cameraKit记录来表明“没有相机套件”,但这似乎太致命了。

或者我的架构错误? 我是否使用 ORM 做了不该做的事情? (我并没有真正使用过 ORM)

谢谢!

I've just started playing with Castle active record as it seems like a gentle way into NHibernate. I really like the idea of the database schema being generate from my classes during development.

I want to do something similar to the following:

[ActiveRecord]
public class Camera : ActiveRecordBase<Camera>
{
    [PrimaryKey]
    public int CameraId {get; set;}
    [Property]
    public int CamKitId {get; set;}
    [Property]
    public string serialNo {get; set;}
}

[ActiveRecord]
public class Tripod : ActiveRecordBase<Tripod>
{
    [PrimaryKey]
    public int TripodId {get; set;}
    [Property]
    public int CamKitId {get; set;}
    [Property]
    public string serialNo {get; set;}
}

[ActiveRecord]
public class CameraKit : ActiveRecordBase<CameraKit>
{
    [PrimaryKey]
    public int CamKitId {get; set;}
    [Property]
    public string description {get; set;}
    [HasMany(Inverse=true, Table="Cameras", ColumnKey="CamKitId")]
    public IList<Camera> Cameras {get; set;}
    [HasMany(Inverse=true, Table="Tripods", ColumnKey="CamKitId")]
    public IList<Camera> Tripods {get; set;}

}

A camerakit should contain any number of tripods and cameras. Camera kits exist independently of cameras and tripods, but are sometimes related.

The problem is, if I use createschema, this will put foreign key constraints on the Camera and Tripod tables. I don't want this, I want to be able to to set CamKitId to null on the tripod and camera tables to indicate that it is not part of a CameraKit.

Is there a way to tell activerecord/nhibernate to still see it as related, without enforcing the integrity? I was thinking I could have a cameraKit record in there to indicate "no camera kit", but it seems like oeverkill.

Or is my schema wrong?
Am I doing something I shouldn't with an ORM? (I've not really used ORMs much)

Thanks!

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

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

发布评论

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

评论(1

葬シ愛 2024-09-06 16:54:24

使用[BelongsTo]来建模多对一关系,例如:

[ActiveRecord]
public class Camera : ActiveRecordBase<Camera>
{
    [PrimaryKey]
    public int CameraId {get; set;}

    [BelongsTo]
    public CameraKit CamKit {get; set;}

    [Property]
    public string serialNo {get; set;}
}

这样,您可以将相机的CamKit设置为null以指示“无相机套件”。

请参阅http://www.castleproject.org/activerecord/documentation /trunk/usersguide/relations/belongsto.html 供参考。

Use [BelongsTo] to model many-to-one relations, e.g.:

[ActiveRecord]
public class Camera : ActiveRecordBase<Camera>
{
    [PrimaryKey]
    public int CameraId {get; set;}

    [BelongsTo]
    public CameraKit CamKit {get; set;}

    [Property]
    public string serialNo {get; set;}
}

This way, you can set a Camera's CamKit to null to indicate "no camera kit".

See http://www.castleproject.org/activerecord/documentation/trunk/usersguide/relations/belongsto.html for reference.

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