开发者

Castle ActiveRecord - schema generation without enforcing referential integrity?

I've just started playing with Castle active record as it seems like a gentle way into NHib开发者_如何学JAVAernate. 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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜