krprasad
krprasad

Reputation: 359

Nhibernate - Inheritance - Zero to one Relationship to child object, how to map? (Fluent API)

I have a Inheritance Hierarchy where Action is parent of ActionCompleted and ActionCancelled. Order class has a zero to one ActionCompleted and ActionCancelled.

the only place I wanted to keep the foreign key is in the action table of the orderId. is that possible? This works fine in the relational world?? There maybe 7-8 ActionTypes and order will have zero to one relationship to all of them.

I am new to ORM and started with EF but so far did not get any help to resolve this issue, now I want to explore this using NHibernate. I know it maybe too much to ask but if anyone has some free time and can write me the mappings for the below scenario it will be appreciated. If you can point me to some sample which uses similar scenario that will be also great help. Since I am just getting started with Nhibernate it may be too much for me to take on inheritance mapping and mapping to the child object.

Below is the link to the code for classes and the DB script, please let me know if you need any more information. Thanks.

Entity Framework - Inheritance - Zero to one Relationship to child object, how to map? (Fluent API)

http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/69d9145a-f7ec-4845-b01a-26e57e9ba16e?prof=required

Upvotes: 0

Views: 351

Answers (1)

Firo
Firo

Reputation: 30803

//Classes
public class Order
{
    public virtual int OrderId { get; set; }
    public virtual string Name { get; set; }
    public virtual ActionCompleted ACO { get; set; }
    public virtual ActionCancelled ACA { get; set; }

}

public class Action
{
    public virtual int ID { get; set; }
    public virtual DateTime ActionDT { get; set; }
    public virtual Order Order { get; set; }
}

public class ActionCompleted : Action
{

}
public class ActionCancelled : Action
{
    public Physician CancelledBy { get; set; }
}

// FluentNHibernate Mappings
public class OrderMap : ClassMap<Order>
{
    public OrderMap()
    {
        Id(x => x.OrderId);

        Map(x => x.Name);
        References(x => x.ACO, "ActionCompletedId");
        References(x => x.ACA, "ActionCanceledId");
    }
}

public class ActionMap : ClassMap<Action>
{
    public ActionMap()
    {
        Id(x => x.ActionID);
        DiscriminateSubclassOn("ActionType");   // if present TPH, otherwise TPC

        Map(x => x.ActionDT);
        References(x => x.Order, "DiagOrderId");
    }
}

public class ActionCompletedMap : SubclassMap<ActionCompleted>
{
    public ActionCompletedMap()
    {
        DiscrimitatorValue("Completed");
    }
}
public class ActionCancelledMap : SubclassMap<>
{
    public ActionCompletedMap()
    {
        DiscrimitatorValue("Completed");
        Refernces(x => x.CancelledBy, "CancelledByPhysician");
    }
}

// factory aka ObjectContext
ISessionFactory factory = Fluently.Configure()
    .DataBase(SqlServerConfiguration.Standard.Connectionstring(connstring))
    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<OrderMap>())
    .BuildSessionFactory();

Update: Update 2:

// FluentNHibernate Mappings
public class OrderMap : ClassMap<Order>
{
    public OrderMap()
    {
        Id(x => x.OrderId);

        Map(x => x.Name);

        Join("ActionTable", join => 
        {
            .KeyColumn("OrderId");
            join.Optional();
            join.Where("ActionType = 'ActionCompleted'");
            join.Component(x => x.ACO, c => 
            {
                c.Map(x => x.ActionID).Not.Insert();
                c.Map(x => x.ActionDT);
                c.ParentReference(x => x.Order);
            }
        }));
        Join("ActionTable", join =>
        {
            join.KeyColumn("OrderId");
            join.Optional();
            join.Where("ActionType = 'ActionCanceled'");
            join.Component(x => x.ACA, c => 
            {
                c.Map(x => x.ActionID).Not.Insert();
                c.Map(x => x.ActionDT);
                c.ParentReference(x => x.Order);
                c.Map(x => x.CanceledBy);
            }
        }));
    }
}

Upvotes: 2

Related Questions