English 中文(简体)
Castle ActiveRecord OneToOne and JoinedBase/Key all together, No SQL Relations Created
原标题:

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 TerminationStaffing 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.





相关问题
Many-To-Many Surrogate Key problem. Please help!

I have a many-to-many relationship with surrogate key The classes are: Insurer - InsurerSection - Section. InsurerSection has one extra attribute: active : bool. How do I access these bool ...

hql get objects where certain property is unique

I am trying to perform an hql query which returns a list of objects with distinct property value. Following is my pseudo code: string hql = @"select distinct m from Merchandise m where ...

nhibernate hql subquery performance

I have written an hql to support paging string hql = @"select distinct mr from MediaResource as mr where mr.Deleted= false ...

ASP.NET MVC - Castle ActiveRecord - Show SQL queries

I m using ASP.NET MVC with Castle ActiveRecord as my persistance layer. I want to know if it s possible to show the SQL queries being executed on my MySQL server. I know it s possible in a Web ...

热门标签