开发者

Castle ActiveRecord OneToOne and JoinedBase/Key all together, No SQL Relations Created

开发者 https://www.devze.com 2022-12-13 03:00 出处:网络
I\'d like to model the following relationship. [JoinedBase] MasterForm{ Guid MasterFormId {get;set;} /* some base properties like modifiedBy etc... */

I'd like to model the following relationship.

[JoinedBase]
MasterForm{
  Guid MasterFormId {get;set;}
 /* some base properties like modifiedBy etc... */
}

[ActiveRecord]
TerminationForm{
   [PrmaryKey(Foreign)]
   Guid MasterFormId {get; set;}
   /* Some more properties specific to terminations */
}

[ActiveRecord("TermStaffing")]
    public class TermStaffing : StaffingBase, ITermStaffing
    {
    }

    public abstract class StaffingBase : EntityBase<StaffingBase>, IStaffingBase
    {
        protected StaffingBase()
        {

        }

        protected StaffingBase(string createdBy)
        {
            this.CreatedBy = createdBy;
        }

        [PrimaryKey(PrimaryKeyType.Foreign)]
        public virtual Guid MasterFormId
        {
            get; set;
        }
}

This all forms the main class inheritance... base form and then some specific forms ..TerminationForm, another form... etc...

And then I was going to hang some other form-sections off each child form. I modeled those child forms as [OneToOne]

I.e. if TerminationForm is analogous to FormOne above.. I have "Staffing" below it.. this is the link.. and its reciprocal link (Note... I've also pulled up some Staffing properties into an abstract base because I have T开发者_StackOverflow社区erminationStaffing and LeaveStaffing)

[OneToOne(MapType = typeof(TermStaffing), Cascade = CascadeEnum.All, PropertyRef = "MasterFormId", ForeignKey = "FK_TerminationFormsStaffing", Constrained = true)]
        public virtual ITermStaffing Staffing

[OneToOne(MapType = typeof(TerminationForm), PropertyRef = "MasterFormId", ForeignKey = "FK_StaffingTerminationForms", Constrained = true)]
        public virtual ITerminationForm TerminationForm

When AR creates the schema.. it properly relates TerminationForm to MasterForm via the relationship that constrains their Primary Key...

However, even though TerminationStaffing table includes a MasterFormId I don't see the relation created. Should I worry about this? Maybe I can just add it after but I was surprised.

I thought about using [BelongsTo] on TerminationStaffing but then what relation goes in TerminationForm (the relation is one to one.. not one to many)

Am I way off base?


It turns out that if you use Constrained = true on the "child" side of a OneToOne relationship that this impacts the order in which records are inserted. It also, I believe, may impact the foreign key relationships which AR adds to your Database.

OneToOne has been a great feature for me to do logical separation of a big datamodel into tables which have less responsibility.

0

精彩评论

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