Fluent NHibernate Mapping a column against one of two columns

筅森魡賤 提交于 2019-12-02 06:57:46

问题


I'm dealing with some legacy vendor code that I can't modify. I'd like to wrap the database with an abstraction layer that is easier to use.

Given the following two tables, I need to create a mapping for Process.Route that will find the matching Route for a given Process, but that can be either dbo.Route.SourceProcessID or dbo.Route.DestinationProcessID:

TABLE [dbo].[Route](
    [RouteID] [bigint] IDENTITY(1,1) NOT NULL,
    [SourceProcessID] [bigint] NOT NULL,
    [DestinationProcessID] [bigint] NOT NULL

TABLE [dbo].[Process](
    [ProcessID] [bigint] IDENTITY(1,1) NOT NULL
)

回答1:


It's not elegant, but I finally came up with the following:

public class Process
{
    public virtual IList<Route> SourceRoutes { get; set; }
    public virtual IList<Route> DestinationRoutes { get; set; }
}

public class ProcessOverride : IAutoMappingOverride<Process>
{
    public void Override(AutoMapping<Process> mapping)
    {
        mapping.HasMany(proc => proc.SourceRoutes).Table("Routes").KeyColumn("SourceID");
        mapping.HasMany(proc => proc.DestinationRoutes).Table("Routes").KeyColumn("DestID");
    }
}


来源:https://stackoverflow.com/questions/1615604/fluent-nhibernate-mapping-a-column-against-one-of-two-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!