我有一个继承层次结构,其中操作是ActionCompleted和ActionCancelled的父级。Order类具有从零到一的ActionCompleted和ActionCancelled。
我唯一想保存外键的地方是在orderId的动作表中。这有可能吗?这在关系世界中工作得很好?可能有7-8个ActionTypes和订单将与所有这些订单有零对一的关系。
我是一个新手,刚开始使用EF,但到目前为止还没有得到任何帮助来解决这个问题,现在我想用NHibernate来探索这个问题。我知道这可能要求太多了,但是如果有人有空闲时间,可以为下面的场景编写映射,我将不胜感激。如果你能给我一些使用类似场景的样本,那也会有很大的帮助。因为我刚刚开始使用Nhibernate,所以继承映射和对子对象的映射对我来说可能太多了。
下面是类和DB脚本代码的链接,如果您需要更多信息,请让我知道。谢谢。
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
发布于 2011-12-01 01:05:23
//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();更新:更新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);
}
}));
}
}https://stackoverflow.com/questions/8328193
复制相似问题